index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = exports.test = exports.it = exports.describe = exports.beforeEach = exports.beforeAll = exports.afterEach = exports.afterAll = void 0;
  6. var _chalk = _interopRequireDefault(require('chalk'));
  7. var _jestEach = require('jest-each');
  8. var _jestMessageUtil = require('jest-message-util');
  9. var _jestUtil = require('jest-util');
  10. var _state = require('./state');
  11. function _interopRequireDefault(obj) {
  12. return obj && obj.__esModule ? obj : {default: obj};
  13. }
  14. /**
  15. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  16. *
  17. * This source code is licensed under the MIT license found in the
  18. * LICENSE file in the root directory of this source tree.
  19. */
  20. const describe = (() => {
  21. const describe = (blockName, blockFn) =>
  22. _dispatchDescribe(blockFn, blockName, describe);
  23. const only = (blockName, blockFn) =>
  24. _dispatchDescribe(blockFn, blockName, only, 'only');
  25. const skip = (blockName, blockFn) =>
  26. _dispatchDescribe(blockFn, blockName, skip, 'skip');
  27. describe.each = (0, _jestEach.bind)(describe, false);
  28. only.each = (0, _jestEach.bind)(only, false);
  29. skip.each = (0, _jestEach.bind)(skip, false);
  30. describe.only = only;
  31. describe.skip = skip;
  32. return describe;
  33. })();
  34. exports.describe = describe;
  35. const _dispatchDescribe = (blockFn, blockName, describeFn, mode) => {
  36. const asyncError = new _jestUtil.ErrorWithStack(undefined, describeFn);
  37. if (blockFn === undefined) {
  38. asyncError.message = `Missing second argument. It must be a callback function.`;
  39. throw asyncError;
  40. }
  41. if (typeof blockFn !== 'function') {
  42. asyncError.message = `Invalid second argument, ${blockFn}. It must be a callback function.`;
  43. throw asyncError;
  44. }
  45. (0, _state.dispatchSync)({
  46. asyncError,
  47. blockName,
  48. mode,
  49. name: 'start_describe_definition'
  50. });
  51. const describeReturn = blockFn(); // TODO throw in Jest 25
  52. if ((0, _jestUtil.isPromise)(describeReturn)) {
  53. console.log(
  54. (0, _jestMessageUtil.formatExecError)(
  55. new _jestUtil.ErrorWithStack(
  56. _chalk.default.yellow(
  57. 'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.\n' +
  58. 'Returning a value from "describe" will fail the test in a future version of Jest.'
  59. ),
  60. describeFn
  61. ),
  62. {
  63. rootDir: '',
  64. testMatch: []
  65. },
  66. {
  67. noStackTrace: false
  68. }
  69. )
  70. );
  71. } else if (describeReturn !== undefined) {
  72. console.log(
  73. (0, _jestMessageUtil.formatExecError)(
  74. new _jestUtil.ErrorWithStack(
  75. _chalk.default.yellow(
  76. 'A "describe" callback must not return a value.\n' +
  77. 'Returning a value from "describe" will fail the test in a future version of Jest.'
  78. ),
  79. describeFn
  80. ),
  81. {
  82. rootDir: '',
  83. testMatch: []
  84. },
  85. {
  86. noStackTrace: false
  87. }
  88. )
  89. );
  90. }
  91. (0, _state.dispatchSync)({
  92. blockName,
  93. mode,
  94. name: 'finish_describe_definition'
  95. });
  96. };
  97. const _addHook = (fn, hookType, hookFn, timeout) => {
  98. const asyncError = new _jestUtil.ErrorWithStack(undefined, hookFn);
  99. if (typeof fn !== 'function') {
  100. asyncError.message =
  101. 'Invalid first argument. It must be a callback function.';
  102. throw asyncError;
  103. }
  104. (0, _state.dispatchSync)({
  105. asyncError,
  106. fn,
  107. hookType,
  108. name: 'add_hook',
  109. timeout
  110. });
  111. }; // Hooks have to pass themselves to the HOF in order for us to trim stack traces.
  112. const beforeEach = (fn, timeout) =>
  113. _addHook(fn, 'beforeEach', beforeEach, timeout);
  114. exports.beforeEach = beforeEach;
  115. const beforeAll = (fn, timeout) =>
  116. _addHook(fn, 'beforeAll', beforeAll, timeout);
  117. exports.beforeAll = beforeAll;
  118. const afterEach = (fn, timeout) =>
  119. _addHook(fn, 'afterEach', afterEach, timeout);
  120. exports.afterEach = afterEach;
  121. const afterAll = (fn, timeout) => _addHook(fn, 'afterAll', afterAll, timeout);
  122. exports.afterAll = afterAll;
  123. const test = (() => {
  124. const test = (testName, fn, timeout) =>
  125. _addTest(testName, undefined, fn, test, timeout);
  126. const skip = (testName, fn, timeout) =>
  127. _addTest(testName, 'skip', fn, skip, timeout);
  128. const only = (testName, fn, timeout) =>
  129. _addTest(testName, 'only', fn, test.only, timeout);
  130. test.todo = (testName, ...rest) => {
  131. if (rest.length > 0 || typeof testName !== 'string') {
  132. throw new _jestUtil.ErrorWithStack(
  133. 'Todo must be called with only a description.',
  134. test.todo
  135. );
  136. }
  137. return _addTest(testName, 'todo', () => {}, test.todo);
  138. };
  139. const _addTest = (testName, mode, fn, testFn, timeout) => {
  140. const asyncError = new _jestUtil.ErrorWithStack(undefined, testFn);
  141. if (typeof testName !== 'string') {
  142. asyncError.message = `Invalid first argument, ${testName}. It must be a string.`;
  143. throw asyncError;
  144. }
  145. if (fn === undefined) {
  146. asyncError.message =
  147. 'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.';
  148. throw asyncError;
  149. }
  150. if (typeof fn !== 'function') {
  151. asyncError.message = `Invalid second argument, ${fn}. It must be a callback function.`;
  152. throw asyncError;
  153. }
  154. return (0, _state.dispatchSync)({
  155. asyncError,
  156. fn,
  157. mode,
  158. name: 'add_test',
  159. testName,
  160. timeout
  161. });
  162. };
  163. test.each = (0, _jestEach.bind)(test);
  164. only.each = (0, _jestEach.bind)(only);
  165. skip.each = (0, _jestEach.bind)(skip);
  166. test.only = only;
  167. test.skip = skip;
  168. return test;
  169. })();
  170. exports.test = test;
  171. const it = test;
  172. exports.it = it;
  173. var _default = {
  174. afterAll,
  175. afterEach,
  176. beforeAll,
  177. beforeEach,
  178. describe,
  179. it,
  180. test
  181. };
  182. exports.default = _default;