run.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _types = require('./types');
  7. var _state = require('./state');
  8. var _utils = require('./utils');
  9. /**
  10. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  11. *
  12. * This source code is licensed under the MIT license found in the
  13. * LICENSE file in the root directory of this source tree.
  14. */
  15. const run = async () => {
  16. const {rootDescribeBlock} = (0, _state.getState)();
  17. await (0, _state.dispatch)({
  18. name: 'run_start'
  19. });
  20. await _runTestsForDescribeBlock(rootDescribeBlock);
  21. await (0, _state.dispatch)({
  22. name: 'run_finish'
  23. });
  24. return (0, _utils.makeRunResult)(
  25. (0, _state.getState)().rootDescribeBlock,
  26. (0, _state.getState)().unhandledErrors
  27. );
  28. };
  29. const _runTestsForDescribeBlock = async describeBlock => {
  30. await (0, _state.dispatch)({
  31. describeBlock,
  32. name: 'run_describe_start'
  33. });
  34. const {beforeAll, afterAll} = (0, _utils.getAllHooksForDescribe)(
  35. describeBlock
  36. );
  37. for (const hook of beforeAll) {
  38. await _callCircusHook({
  39. describeBlock,
  40. hook
  41. });
  42. } // Tests that fail and are retried we run after other tests
  43. const retryTimes = parseInt(global[_types.RETRY_TIMES], 10) || 0;
  44. const deferredRetryTests = [];
  45. for (const child of describeBlock.children) {
  46. switch (child.type) {
  47. case 'describeBlock': {
  48. await _runTestsForDescribeBlock(child);
  49. break;
  50. }
  51. case 'test': {
  52. const hasErrorsBeforeTestRun = child.errors.length > 0;
  53. await _runTest(child);
  54. if (
  55. hasErrorsBeforeTestRun === false &&
  56. retryTimes > 0 &&
  57. child.errors.length > 0
  58. ) {
  59. deferredRetryTests.push(child);
  60. }
  61. break;
  62. }
  63. }
  64. } // Re-run failed tests n-times if configured
  65. for (const test of deferredRetryTests) {
  66. let numRetriesAvailable = retryTimes;
  67. while (numRetriesAvailable > 0 && test.errors.length > 0) {
  68. // Clear errors so retries occur
  69. await (0, _state.dispatch)({
  70. name: 'test_retry',
  71. test
  72. });
  73. await _runTest(test);
  74. numRetriesAvailable--;
  75. }
  76. }
  77. for (const hook of afterAll) {
  78. await _callCircusHook({
  79. describeBlock,
  80. hook
  81. });
  82. }
  83. await (0, _state.dispatch)({
  84. describeBlock,
  85. name: 'run_describe_finish'
  86. });
  87. };
  88. const _runTest = async test => {
  89. await (0, _state.dispatch)({
  90. name: 'test_start',
  91. test
  92. });
  93. const testContext = Object.create(null);
  94. const {hasFocusedTests, testNamePattern} = (0, _state.getState)();
  95. const isSkipped =
  96. test.mode === 'skip' ||
  97. (hasFocusedTests && test.mode !== 'only') ||
  98. (testNamePattern && !testNamePattern.test((0, _utils.getTestID)(test)));
  99. if (isSkipped) {
  100. await (0, _state.dispatch)({
  101. name: 'test_skip',
  102. test
  103. });
  104. return;
  105. }
  106. if (test.mode === 'todo') {
  107. await (0, _state.dispatch)({
  108. name: 'test_todo',
  109. test
  110. });
  111. return;
  112. }
  113. const {afterEach, beforeEach} = (0, _utils.getEachHooksForTest)(test);
  114. for (const hook of beforeEach) {
  115. if (test.errors.length) {
  116. // If any of the before hooks failed already, we don't run any
  117. // hooks after that.
  118. break;
  119. }
  120. await _callCircusHook({
  121. hook,
  122. test,
  123. testContext
  124. });
  125. }
  126. await _callCircusTest(test, testContext);
  127. for (const hook of afterEach) {
  128. await _callCircusHook({
  129. hook,
  130. test,
  131. testContext
  132. });
  133. } // `afterAll` hooks should not affect test status (pass or fail), because if
  134. // we had a global `afterAll` hook it would block all existing tests until
  135. // this hook is executed. So we dispatch `test_done` right away.
  136. await (0, _state.dispatch)({
  137. name: 'test_done',
  138. test
  139. });
  140. };
  141. const _callCircusHook = async ({hook, test, describeBlock, testContext}) => {
  142. await (0, _state.dispatch)({
  143. hook,
  144. name: 'hook_start'
  145. });
  146. const timeout = hook.timeout || (0, _state.getState)().testTimeout;
  147. try {
  148. await (0, _utils.callAsyncCircusFn)(hook, testContext, {
  149. isHook: true,
  150. timeout
  151. });
  152. await (0, _state.dispatch)({
  153. describeBlock,
  154. hook,
  155. name: 'hook_success',
  156. test
  157. });
  158. } catch (error) {
  159. await (0, _state.dispatch)({
  160. describeBlock,
  161. error,
  162. hook,
  163. name: 'hook_failure',
  164. test
  165. });
  166. }
  167. };
  168. const _callCircusTest = async (test, testContext) => {
  169. await (0, _state.dispatch)({
  170. name: 'test_fn_start',
  171. test
  172. });
  173. const timeout = test.timeout || (0, _state.getState)().testTimeout;
  174. (0, _utils.invariant)(
  175. test.fn,
  176. `Tests with no 'fn' should have 'mode' set to 'skipped'`
  177. );
  178. if (test.errors.length) {
  179. return; // We don't run the test if there's already an error in before hooks.
  180. }
  181. try {
  182. await (0, _utils.callAsyncCircusFn)(test, testContext, {
  183. isHook: false,
  184. timeout
  185. });
  186. await (0, _state.dispatch)({
  187. name: 'test_fn_success',
  188. test
  189. });
  190. } catch (error) {
  191. await (0, _state.dispatch)({
  192. error,
  193. name: 'test_fn_failure',
  194. test
  195. });
  196. }
  197. };
  198. var _default = run;
  199. exports.default = _default;