forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-circus.ts
More file actions
141 lines (117 loc) · 3.82 KB
/
run-circus.ts
File metadata and controls
141 lines (117 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
/* eslint-disable no-use-before-define, no-restricted-syntax, no-await-in-loop */
import { getState, dispatch } from 'jest-circus/build/state';
import {
callAsyncFn,
getAllHooksForDescribe,
getEachHooksForTest,
makeTestResults,
} from 'jest-circus/build/utils';
import { SnapshotState } from 'jest-snapshot';
import expect from 'jest-matchers';
import {
TestEntry,
TestResults,
TestContext,
Hook,
DescribeBlock,
} from './types';
const currentDescribeBlocks = [];
const run = async (): Promise<TestResults> => {
const { rootDescribeBlock } = getState();
currentDescribeBlocks.length = 0;
dispatch({ name: 'run_start' });
await _runTestsForDescribeBlock(rootDescribeBlock);
dispatch({ name: 'run_finish' });
return makeTestResults(getState().rootDescribeBlock);
};
const _setGlobalState = (test: TestEntry) => {
const { testPath: currentTestPath } = expect.getState();
const [testPath, testName] = test.name.split(':#:');
// remove root block
const [, ...describeBlocks] = [...currentDescribeBlocks];
const describeName =
describeBlocks.length > 0 ? describeBlocks.join(' ') + ' ' : '';
const currentTestName = describeName + testName;
const update: {
// @ts-ignore
snapshotState?: SnapshotState;
testPath?: string;
currentTestName: string;
} = { currentTestName };
if (testPath == null || currentTestPath !== testPath) {
// @ts-ignore
update.snapshotState = new SnapshotState(testPath, {
expand: true,
updateSnapshot: 'none',
});
update.testPath = testPath;
}
expect.setState(update);
};
const _runTestsForDescribeBlock = async (describeBlock: DescribeBlock) => {
currentDescribeBlocks.push(describeBlock.name);
dispatch({ describeBlock, name: 'run_describe_start' });
const { beforeAll, afterAll } = getAllHooksForDescribe(describeBlock);
for (const hook of beforeAll) {
_callHook(hook);
}
for (const test of describeBlock.tests) {
await _runTest(test);
}
for (const child of describeBlock.children) {
await _runTestsForDescribeBlock(child);
}
for (const hook of afterAll) {
_callHook(hook);
}
dispatch({ describeBlock, name: 'run_describe_finish' });
currentDescribeBlocks.pop();
};
const _runTest = async (test: TestEntry): Promise<void> => {
const testContext = Object.create(null);
const isSkipped =
test.mode === 'skip' ||
(getState().hasFocusedTests && test.mode !== 'only');
if (isSkipped) {
dispatch({ name: 'test_skip', test });
return;
}
const { afterEach, beforeEach } = getEachHooksForTest(test);
for (const hook of beforeEach) {
await _callHook(hook, testContext);
}
await _callTest(test, testContext);
for (const hook of afterEach) {
await _callHook(hook, testContext);
}
};
const _callHook = (hook: Hook, testContext?: TestContext): Promise<any> => {
dispatch({ hook, name: 'hook_start' });
const { testTimeout: timeout } = getState();
return callAsyncFn(hook.fn, testContext, { isHook: true, timeout })
.then(() => dispatch({ hook, name: 'hook_success' }))
.catch(error => dispatch({ error, hook, name: 'hook_failure' }));
};
const _callTest = async (
test: TestEntry,
testContext: TestContext
): Promise<any> => {
dispatch({ name: 'test_start', test });
const { testTimeout: timeout } = getState();
if (!test.fn) {
throw Error(`Tests with no 'fn' should have 'mode' set to 'skipped'`);
}
_setGlobalState(test);
return callAsyncFn(test.fn, testContext, { isHook: false, timeout })
.then(() => dispatch({ name: 'test_success', test }))
.catch(error => dispatch({ error, name: 'test_failure', test }));
};
export default run;