index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.describe =
  6. exports.default =
  7. exports.beforeEach =
  8. exports.beforeAll =
  9. exports.afterEach =
  10. exports.afterAll =
  11. void 0;
  12. Object.defineProperty(exports, 'getState', {
  13. enumerable: true,
  14. get: function () {
  15. return _state.getState;
  16. }
  17. });
  18. exports.it = void 0;
  19. Object.defineProperty(exports, 'resetState', {
  20. enumerable: true,
  21. get: function () {
  22. return _state.resetState;
  23. }
  24. });
  25. Object.defineProperty(exports, 'run', {
  26. enumerable: true,
  27. get: function () {
  28. return _run.default;
  29. }
  30. });
  31. Object.defineProperty(exports, 'setState', {
  32. enumerable: true,
  33. get: function () {
  34. return _state.setState;
  35. }
  36. });
  37. exports.test = void 0;
  38. var _jestEach = require('jest-each');
  39. var _jestUtil = require('jest-util');
  40. var _state = require('./state');
  41. var _run = _interopRequireDefault(require('./run'));
  42. function _interopRequireDefault(obj) {
  43. return obj && obj.__esModule ? obj : {default: obj};
  44. }
  45. /**
  46. * Copyright (c) Meta Platforms, Inc. and affiliates.
  47. *
  48. * This source code is licensed under the MIT license found in the
  49. * LICENSE file in the root directory of this source tree.
  50. */
  51. const describe = (() => {
  52. const describe = (blockName, blockFn) =>
  53. _dispatchDescribe(blockFn, blockName, describe);
  54. const only = (blockName, blockFn) =>
  55. _dispatchDescribe(blockFn, blockName, only, 'only');
  56. const skip = (blockName, blockFn) =>
  57. _dispatchDescribe(blockFn, blockName, skip, 'skip');
  58. describe.each = (0, _jestEach.bind)(describe, false);
  59. only.each = (0, _jestEach.bind)(only, false);
  60. skip.each = (0, _jestEach.bind)(skip, false);
  61. describe.only = only;
  62. describe.skip = skip;
  63. return describe;
  64. })();
  65. exports.describe = describe;
  66. const _dispatchDescribe = (blockFn, blockName, describeFn, mode) => {
  67. const asyncError = new _jestUtil.ErrorWithStack(undefined, describeFn);
  68. if (blockFn === undefined) {
  69. asyncError.message =
  70. 'Missing second argument. It must be a callback function.';
  71. throw asyncError;
  72. }
  73. if (typeof blockFn !== 'function') {
  74. asyncError.message = `Invalid second argument, ${blockFn}. It must be a callback function.`;
  75. throw asyncError;
  76. }
  77. try {
  78. blockName = (0, _jestUtil.convertDescriptorToString)(blockName);
  79. } catch (error) {
  80. asyncError.message = error.message;
  81. throw asyncError;
  82. }
  83. (0, _state.dispatchSync)({
  84. asyncError,
  85. blockName,
  86. mode,
  87. name: 'start_describe_definition'
  88. });
  89. const describeReturn = blockFn();
  90. if ((0, _jestUtil.isPromise)(describeReturn)) {
  91. throw new _jestUtil.ErrorWithStack(
  92. 'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.',
  93. describeFn
  94. );
  95. } else if (describeReturn !== undefined) {
  96. throw new _jestUtil.ErrorWithStack(
  97. 'A "describe" callback must not return a value.',
  98. describeFn
  99. );
  100. }
  101. (0, _state.dispatchSync)({
  102. blockName,
  103. mode,
  104. name: 'finish_describe_definition'
  105. });
  106. };
  107. const _addHook = (fn, hookType, hookFn, timeout) => {
  108. const asyncError = new _jestUtil.ErrorWithStack(undefined, hookFn);
  109. if (typeof fn !== 'function') {
  110. asyncError.message =
  111. 'Invalid first argument. It must be a callback function.';
  112. throw asyncError;
  113. }
  114. (0, _state.dispatchSync)({
  115. asyncError,
  116. fn,
  117. hookType,
  118. name: 'add_hook',
  119. timeout
  120. });
  121. };
  122. // Hooks have to pass themselves to the HOF in order for us to trim stack traces.
  123. const beforeEach = (fn, timeout) =>
  124. _addHook(fn, 'beforeEach', beforeEach, timeout);
  125. exports.beforeEach = beforeEach;
  126. const beforeAll = (fn, timeout) =>
  127. _addHook(fn, 'beforeAll', beforeAll, timeout);
  128. exports.beforeAll = beforeAll;
  129. const afterEach = (fn, timeout) =>
  130. _addHook(fn, 'afterEach', afterEach, timeout);
  131. exports.afterEach = afterEach;
  132. const afterAll = (fn, timeout) => _addHook(fn, 'afterAll', afterAll, timeout);
  133. exports.afterAll = afterAll;
  134. const test = (() => {
  135. const test = (testName, fn, timeout) =>
  136. _addTest(testName, undefined, false, fn, test, timeout);
  137. const skip = (testName, fn, timeout) =>
  138. _addTest(testName, 'skip', false, fn, skip, timeout);
  139. const only = (testName, fn, timeout) =>
  140. _addTest(testName, 'only', false, fn, test.only, timeout);
  141. const concurrentTest = (testName, fn, timeout) =>
  142. _addTest(testName, undefined, true, fn, concurrentTest, timeout);
  143. const concurrentOnly = (testName, fn, timeout) =>
  144. _addTest(testName, 'only', true, fn, concurrentOnly, timeout);
  145. const bindFailing = (concurrent, mode) => {
  146. const failing = (testName, fn, timeout, eachError) =>
  147. _addTest(
  148. testName,
  149. mode,
  150. concurrent,
  151. fn,
  152. failing,
  153. timeout,
  154. true,
  155. eachError
  156. );
  157. failing.each = (0, _jestEach.bind)(failing, false, true);
  158. return failing;
  159. };
  160. test.todo = (testName, ...rest) => {
  161. if (rest.length > 0 || typeof testName !== 'string') {
  162. throw new _jestUtil.ErrorWithStack(
  163. 'Todo must be called with only a description.',
  164. test.todo
  165. );
  166. }
  167. // eslint-disable-next-line @typescript-eslint/no-empty-function
  168. return _addTest(testName, 'todo', false, () => {}, test.todo);
  169. };
  170. const _addTest = (
  171. testName,
  172. mode,
  173. concurrent,
  174. fn,
  175. testFn,
  176. timeout,
  177. failing,
  178. asyncError = new _jestUtil.ErrorWithStack(undefined, testFn)
  179. ) => {
  180. try {
  181. testName = (0, _jestUtil.convertDescriptorToString)(testName);
  182. } catch (error) {
  183. asyncError.message = error.message;
  184. throw asyncError;
  185. }
  186. if (fn === undefined) {
  187. asyncError.message =
  188. 'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.';
  189. throw asyncError;
  190. }
  191. if (typeof fn !== 'function') {
  192. asyncError.message = `Invalid second argument, ${fn}. It must be a callback function.`;
  193. throw asyncError;
  194. }
  195. return (0, _state.dispatchSync)({
  196. asyncError,
  197. concurrent,
  198. failing: failing === undefined ? false : failing,
  199. fn,
  200. mode,
  201. name: 'add_test',
  202. testName,
  203. timeout
  204. });
  205. };
  206. test.each = (0, _jestEach.bind)(test);
  207. only.each = (0, _jestEach.bind)(only);
  208. skip.each = (0, _jestEach.bind)(skip);
  209. concurrentTest.each = (0, _jestEach.bind)(concurrentTest, false);
  210. concurrentOnly.each = (0, _jestEach.bind)(concurrentOnly, false);
  211. only.failing = bindFailing(false, 'only');
  212. skip.failing = bindFailing(false, 'skip');
  213. test.failing = bindFailing(false);
  214. test.only = only;
  215. test.skip = skip;
  216. test.concurrent = concurrentTest;
  217. concurrentTest.only = concurrentOnly;
  218. concurrentTest.skip = skip;
  219. concurrentTest.failing = bindFailing(true);
  220. concurrentOnly.failing = bindFailing(true, 'only');
  221. return test;
  222. })();
  223. exports.test = test;
  224. const it = test;
  225. exports.it = it;
  226. var _default = {
  227. afterAll,
  228. afterEach,
  229. beforeAll,
  230. beforeEach,
  231. describe,
  232. it,
  233. test
  234. };
  235. exports.default = _default;