Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

eventHandler.js 9.4KB

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