index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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) Facebook, Inc. and its affiliates. All Rights Reserved.
  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. (0, _state.dispatchSync)({
  78. asyncError,
  79. blockName,
  80. mode,
  81. name: 'start_describe_definition'
  82. });
  83. const describeReturn = blockFn();
  84. if ((0, _jestUtil.isPromise)(describeReturn)) {
  85. throw new _jestUtil.ErrorWithStack(
  86. 'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.',
  87. describeFn
  88. );
  89. } else if (describeReturn !== undefined) {
  90. throw new _jestUtil.ErrorWithStack(
  91. 'A "describe" callback must not return a value.',
  92. describeFn
  93. );
  94. }
  95. (0, _state.dispatchSync)({
  96. blockName,
  97. mode,
  98. name: 'finish_describe_definition'
  99. });
  100. };
  101. const _addHook = (fn, hookType, hookFn, timeout) => {
  102. const asyncError = new _jestUtil.ErrorWithStack(undefined, hookFn);
  103. if (typeof fn !== 'function') {
  104. asyncError.message =
  105. 'Invalid first argument. It must be a callback function.';
  106. throw asyncError;
  107. }
  108. (0, _state.dispatchSync)({
  109. asyncError,
  110. fn,
  111. hookType,
  112. name: 'add_hook',
  113. timeout
  114. });
  115. }; // Hooks have to pass themselves to the HOF in order for us to trim stack traces.
  116. const beforeEach = (fn, timeout) =>
  117. _addHook(fn, 'beforeEach', beforeEach, timeout);
  118. exports.beforeEach = beforeEach;
  119. const beforeAll = (fn, timeout) =>
  120. _addHook(fn, 'beforeAll', beforeAll, timeout);
  121. exports.beforeAll = beforeAll;
  122. const afterEach = (fn, timeout) =>
  123. _addHook(fn, 'afterEach', afterEach, timeout);
  124. exports.afterEach = afterEach;
  125. const afterAll = (fn, timeout) => _addHook(fn, 'afterAll', afterAll, timeout);
  126. exports.afterAll = afterAll;
  127. const test = (() => {
  128. const test = (testName, fn, timeout) =>
  129. _addTest(testName, undefined, fn, test, timeout);
  130. const skip = (testName, fn, timeout) =>
  131. _addTest(testName, 'skip', fn, skip, timeout);
  132. const only = (testName, fn, timeout) =>
  133. _addTest(testName, 'only', fn, test.only, timeout);
  134. test.todo = (testName, ...rest) => {
  135. if (rest.length > 0 || typeof testName !== 'string') {
  136. throw new _jestUtil.ErrorWithStack(
  137. 'Todo must be called with only a description.',
  138. test.todo
  139. );
  140. }
  141. return _addTest(testName, 'todo', () => {}, test.todo);
  142. };
  143. const _addTest = (testName, mode, fn, testFn, timeout) => {
  144. const asyncError = new _jestUtil.ErrorWithStack(undefined, testFn);
  145. if (typeof testName !== 'string') {
  146. asyncError.message = `Invalid first argument, ${testName}. It must be a string.`;
  147. throw asyncError;
  148. }
  149. if (fn === undefined) {
  150. asyncError.message =
  151. 'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.';
  152. throw asyncError;
  153. }
  154. if (typeof fn !== 'function') {
  155. asyncError.message = `Invalid second argument, ${fn}. It must be a callback function.`;
  156. throw asyncError;
  157. }
  158. return (0, _state.dispatchSync)({
  159. asyncError,
  160. fn,
  161. mode,
  162. name: 'add_test',
  163. testName,
  164. timeout
  165. });
  166. };
  167. test.each = (0, _jestEach.bind)(test);
  168. only.each = (0, _jestEach.bind)(only);
  169. skip.each = (0, _jestEach.bind)(skip);
  170. test.only = only;
  171. test.skip = skip;
  172. return test;
  173. })();
  174. exports.test = test;
  175. const it = test;
  176. exports.it = it;
  177. var _default = {
  178. afterAll,
  179. afterEach,
  180. beforeAll,
  181. beforeEach,
  182. describe,
  183. it,
  184. test
  185. };
  186. exports.default = _default;