eventHandler.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _types = require('./types');
  7. var _utils = require('./utils');
  8. var _globalErrorHandlers = require('./globalErrorHandlers');
  9. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  10. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  11. var jestNow = global[Symbol.for('jest-native-now')] || global.Date.now;
  12. // TODO: investigate why a shorter (event, state) signature results into TS7006 compiler error
  13. const eventHandler = (event, state) => {
  14. switch (event.name) {
  15. case 'include_test_location_in_result': {
  16. state.includeTestLocationInResult = true;
  17. break;
  18. }
  19. case 'hook_start': {
  20. break;
  21. }
  22. case 'start_describe_definition': {
  23. const {blockName, mode} = event;
  24. const {currentDescribeBlock, currentlyRunningTest} = state;
  25. if (currentlyRunningTest) {
  26. currentlyRunningTest.errors.push(
  27. new Error(
  28. `Cannot nest a describe inside a test. Describe block "${blockName}" cannot run because it is nested within "${currentlyRunningTest.name}".`
  29. )
  30. );
  31. break;
  32. }
  33. const describeBlock = (0, _utils.makeDescribe)(
  34. blockName,
  35. currentDescribeBlock,
  36. mode
  37. );
  38. currentDescribeBlock.children.push(describeBlock);
  39. state.currentDescribeBlock = describeBlock;
  40. break;
  41. }
  42. case 'finish_describe_definition': {
  43. const {currentDescribeBlock} = state;
  44. (0, _utils.invariant)(
  45. currentDescribeBlock,
  46. `currentDescribeBlock must be there`
  47. );
  48. if (!(0, _utils.describeBlockHasTests)(currentDescribeBlock)) {
  49. currentDescribeBlock.hooks.forEach(hook => {
  50. hook.asyncError.message = `Invalid: ${hook.type}() may not be used in a describe block containing no tests.`;
  51. state.unhandledErrors.push(hook.asyncError);
  52. });
  53. } // pass mode of currentDescribeBlock to tests
  54. // but do not when there is already a single test with "only" mode
  55. const shouldPassMode = !(
  56. currentDescribeBlock.mode === 'only' &&
  57. currentDescribeBlock.children.some(
  58. child => child.type === 'test' && child.mode === 'only'
  59. )
  60. );
  61. if (shouldPassMode) {
  62. currentDescribeBlock.children.forEach(child => {
  63. if (child.type === 'test' && !child.mode) {
  64. child.mode = currentDescribeBlock.mode;
  65. }
  66. });
  67. }
  68. if (
  69. !state.hasFocusedTests &&
  70. currentDescribeBlock.children.some(
  71. child => child.type === 'test' && child.mode === 'only'
  72. )
  73. ) {
  74. state.hasFocusedTests = true;
  75. }
  76. if (currentDescribeBlock.parent) {
  77. state.currentDescribeBlock = currentDescribeBlock.parent;
  78. }
  79. break;
  80. }
  81. case 'add_hook': {
  82. const {currentDescribeBlock, currentlyRunningTest, hasStarted} = state;
  83. const {asyncError, fn, hookType: type, timeout} = event;
  84. if (currentlyRunningTest) {
  85. currentlyRunningTest.errors.push(
  86. new Error(
  87. `Hooks cannot be defined inside tests. Hook of type "${type}" is nested within "${currentlyRunningTest.name}".`
  88. )
  89. );
  90. break;
  91. } else if (hasStarted) {
  92. state.unhandledErrors.push(
  93. new Error(
  94. 'Cannot add a hook after tests have started running. Hooks must be defined synchronously.'
  95. )
  96. );
  97. break;
  98. }
  99. const parent = currentDescribeBlock;
  100. currentDescribeBlock.hooks.push({
  101. asyncError,
  102. fn,
  103. parent,
  104. timeout,
  105. type
  106. });
  107. break;
  108. }
  109. case 'add_test': {
  110. const {currentDescribeBlock, currentlyRunningTest, hasStarted} = state;
  111. const {asyncError, fn, mode, testName: name, timeout} = event;
  112. if (currentlyRunningTest) {
  113. currentlyRunningTest.errors.push(
  114. new Error(
  115. `Tests cannot be nested. Test "${name}" cannot run because it is nested within "${currentlyRunningTest.name}".`
  116. )
  117. );
  118. break;
  119. } else if (hasStarted) {
  120. state.unhandledErrors.push(
  121. new Error(
  122. 'Cannot add a test after tests have started running. Tests must be defined synchronously.'
  123. )
  124. );
  125. break;
  126. }
  127. const test = (0, _utils.makeTest)(
  128. fn,
  129. mode,
  130. name,
  131. currentDescribeBlock,
  132. timeout,
  133. asyncError
  134. );
  135. if (test.mode === 'only') {
  136. state.hasFocusedTests = true;
  137. }
  138. currentDescribeBlock.children.push(test);
  139. currentDescribeBlock.tests.push(test);
  140. break;
  141. }
  142. case 'hook_failure': {
  143. const {test, describeBlock, error, hook} = event;
  144. const {asyncError, type} = hook;
  145. if (type === 'beforeAll') {
  146. (0, _utils.invariant)(describeBlock, 'always present for `*All` hooks');
  147. (0, _utils.addErrorToEachTestUnderDescribe)(
  148. describeBlock,
  149. error,
  150. asyncError
  151. );
  152. } else if (type === 'afterAll') {
  153. // Attaching `afterAll` errors to each test makes execution flow
  154. // too complicated, so we'll consider them to be global.
  155. state.unhandledErrors.push([error, asyncError]);
  156. } else {
  157. (0, _utils.invariant)(test, 'always present for `*Each` hooks');
  158. test.errors.push([error, asyncError]);
  159. }
  160. break;
  161. }
  162. case 'test_skip': {
  163. event.test.status = 'skip';
  164. break;
  165. }
  166. case 'test_todo': {
  167. event.test.status = 'todo';
  168. break;
  169. }
  170. case 'test_done': {
  171. event.test.duration = (0, _utils.getTestDuration)(event.test);
  172. event.test.status = 'done';
  173. state.currentlyRunningTest = null;
  174. break;
  175. }
  176. case 'test_start': {
  177. state.currentlyRunningTest = event.test;
  178. event.test.startedAt = jestNow();
  179. event.test.invocations += 1;
  180. break;
  181. }
  182. case 'test_fn_failure': {
  183. const {
  184. error,
  185. test: {asyncError}
  186. } = event;
  187. event.test.errors.push([error, asyncError]);
  188. break;
  189. }
  190. case 'test_retry': {
  191. event.test.errors = [];
  192. break;
  193. }
  194. case 'run_start': {
  195. state.hasStarted = true;
  196. global[_types.TEST_TIMEOUT_SYMBOL] &&
  197. (state.testTimeout = global[_types.TEST_TIMEOUT_SYMBOL]);
  198. break;
  199. }
  200. case 'run_finish': {
  201. break;
  202. }
  203. case 'setup': {
  204. // Uncaught exception handlers should be defined on the parent process
  205. // object. If defined on the VM's process object they just no op and let
  206. // the parent process crash. It might make sense to return a `dispatch`
  207. // function to the parent process and register handlers there instead, but
  208. // i'm not sure if this is works. For now i just replicated whatever
  209. // jasmine was doing -- dabramov
  210. state.parentProcess = event.parentProcess;
  211. (0, _utils.invariant)(state.parentProcess);
  212. state.originalGlobalErrorHandlers = (0,
  213. _globalErrorHandlers.injectGlobalErrorHandlers)(state.parentProcess);
  214. if (event.testNamePattern) {
  215. state.testNamePattern = new RegExp(event.testNamePattern, 'i');
  216. }
  217. break;
  218. }
  219. case 'teardown': {
  220. (0, _utils.invariant)(state.originalGlobalErrorHandlers);
  221. (0, _utils.invariant)(state.parentProcess);
  222. (0, _globalErrorHandlers.restoreGlobalErrorHandlers)(
  223. state.parentProcess,
  224. state.originalGlobalErrorHandlers
  225. );
  226. break;
  227. }
  228. case 'error': {
  229. // It's very likely for long-running async tests to throw errors. In this
  230. // case we want to catch them and fail the current test. At the same time
  231. // there's a possibility that one test sets a long timeout, that will
  232. // eventually throw after this test finishes but during some other test
  233. // execution, which will result in one test's error failing another test.
  234. // In any way, it should be possible to track where the error was thrown
  235. // from.
  236. state.currentlyRunningTest
  237. ? state.currentlyRunningTest.errors.push(event.error)
  238. : state.unhandledErrors.push(event.error);
  239. break;
  240. }
  241. }
  242. };
  243. var _default = eventHandler;
  244. exports.default = _default;