-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
testUtils.ts
94 lines (79 loc) · 2.56 KB
/
testUtils.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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {createHash} from 'crypto';
import {tmpdir} from 'os';
import * as path from 'path';
import {ExecaSyncReturnValue, sync as spawnSync} from 'execa';
import * as fs from 'graceful-fs';
const CIRCUS_PATH = require.resolve('../').replace(/\\/g, '\\\\');
const CIRCUS_RUN_PATH = require.resolve('../run').replace(/\\/g, '\\\\');
const CIRCUS_STATE_PATH = require.resolve('../state').replace(/\\/g, '\\\\');
const TEST_EVENT_HANDLER_PATH = require
.resolve('./testEventHandler')
.replace(/\\/g, '\\\\');
const BABEL_REGISTER_PATH = require
.resolve('@babel/register')
.replace(/\\/g, '\\\\');
interface Result extends ExecaSyncReturnValue {
status: number;
error: string;
}
export const runTest = (
source: string,
opts?: {seed?: number; randomize?: boolean},
) => {
const filename = createHash('sha1')
.update(source)
.digest('hex')
.substring(0, 32);
const tmpFilename = path.join(tmpdir(), filename);
const content = `
require('${BABEL_REGISTER_PATH}')({extensions: [".js", ".ts"]});
const circus = require('${CIRCUS_PATH}');
global.test = circus.test;
global.describe = circus.describe;
global.beforeEach = circus.beforeEach;
global.afterEach = circus.afterEach;
global.beforeAll = circus.beforeAll;
global.afterAll = circus.afterAll;
const testEventHandler = require('${TEST_EVENT_HANDLER_PATH}').default;
const {addEventHandler, removeEventHandler, getState} = require('${CIRCUS_STATE_PATH}');
getState().randomize = ${opts?.randomize};
getState().seed = ${opts?.seed ?? 0};
addEventHandler(testEventHandler);
${source};
const run = require('${CIRCUS_RUN_PATH}').default;
run();
`;
fs.writeFileSync(tmpFilename, content);
const result = spawnSync('node', [tmpFilename], {
cwd: process.cwd(),
}) as Result;
// For compat with cross-spawn
result.status = result.exitCode;
if (result.status !== 0) {
const message = `
STDOUT: ${result.stdout && result.stdout.toString()}
STDERR: ${result.stderr && result.stderr.toString()}
STATUS: ${result.status}
ERROR: ${String(result.error)}
`;
throw new Error(message);
}
result.stdout = String(result.stdout);
result.stderr = String(result.stderr);
fs.unlinkSync(tmpFilename);
if (result.stderr) {
throw new Error(
`
Unexpected stderr:
${result.stderr}
`,
);
}
return result;
};