-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
105 lines (93 loc) · 2.44 KB
/
index.ts
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
import type {
Rstest,
RunWorkerOptions,
TestFileResult,
WorkerState,
} from '../../types';
import { globalApis } from '../../utils/constants';
import { undoSerializableConfig } from '../../utils/helper';
import { formatTestError } from '../util';
import { loadModule } from './loadModule';
import { createForksRpcOptions, createRuntimeRpc } from './rpc';
import { RstestSnapshotEnvironment } from './snapshot';
const getGlobalApi = (api: Rstest) => {
return globalApis.reduce<{
[key in keyof Rstest]?: Rstest[key];
}>((apis, key) => {
apis[key] = api[key] as any;
return apis;
}, {});
};
const runInPool = async ({
entryInfo: { filePath, originPath },
setupEntries,
assetFiles,
sourceMaps,
updateSnapshot,
context,
}: RunWorkerOptions['options']): Promise<TestFileResult> => {
context.normalizedConfig = undoSerializableConfig(context.normalizedConfig);
const { rpc } = createRuntimeRpc(createForksRpcOptions());
const codeContent = assetFiles[filePath]!;
const {
normalizedConfig: { globals },
} = context;
const workerState: WorkerState = {
...context,
snapshotOptions: {
updateSnapshot,
snapshotEnvironment: new RstestSnapshotEnvironment({
sourceMaps,
}),
},
filePath,
sourcePath: originPath,
environment: 'node',
};
const { createRstestRuntime } = await import('../api');
const { api, runner } = createRstestRuntime(workerState);
const rstestContext = {
global: {
'@rstest/core': api,
},
...(globals ? getGlobalApi(api) : {}),
};
try {
// run setup files
for (const { filePath, originPath } of setupEntries) {
const setupCodeContent = assetFiles[filePath]!;
await loadModule({
codeContent: setupCodeContent,
distPath: filePath,
originPath: originPath,
rstestContext,
assetFiles,
});
}
await loadModule({
codeContent,
distPath: filePath,
originPath,
rstestContext,
assetFiles,
});
const results = await runner.runTest(originPath, {
onTestFileStart: async (test) => {
await rpc.onTestFileStart(test);
},
onTestCaseResult: async (result) => {
await rpc.onTestCaseResult(result);
},
});
return results;
} catch (err) {
return {
testPath: originPath,
status: 'fail',
name: '',
results: [],
errors: formatTestError(err),
};
}
};
export default runInPool;