forked from jestjs/jest
/
runJest.ts
166 lines (145 loc) Β· 4.46 KB
/
runJest.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*
*/
import path from 'path';
import fs from 'fs';
import execa, {ExecaChildProcess, ExecaReturns} from 'execa';
import {Writable} from 'readable-stream';
import stripAnsi from 'strip-ansi';
import {normalizeIcons} from './Utils';
const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js');
type RunJestOptions = {
nodeOptions?: string;
nodePath?: string;
skipPkgJsonCheck?: boolean; // don't complain if can't find package.json
stripAnsi?: boolean; // remove colors from stdout and stderr,
timeout?: number; // kill the Jest process after X milliseconds
};
// return the result of the spawned process:
// [ 'status', 'signal', 'output', 'pid', 'stdout', 'stderr',
// 'envPairs', 'options', 'args', 'file' ]
export default function runJest(
dir: string,
args?: Array<string>,
options: RunJestOptions = {},
) {
return normalizeResult(spawnJest(dir, args, options), options);
}
function spawnJest(
dir: string,
args?: Array<string>,
options?: RunJestOptions,
spawnAsync?: false,
): ExecaReturns;
function spawnJest(
dir: string,
args?: Array<string>,
options?: RunJestOptions,
spawnAsync?: true,
): ExecaChildProcess;
// Spawns Jest and returns either a Promise (if spawnAsync is true) or the completed child process
function spawnJest(
dir: string,
args?: Array<string>,
options: RunJestOptions = {},
spawnAsync: boolean = false,
): ExecaReturns | ExecaChildProcess {
const isRelative = !path.isAbsolute(dir);
if (isRelative) {
dir = path.resolve(__dirname, dir);
}
const localPackageJson = path.resolve(dir, 'package.json');
if (!options.skipPkgJsonCheck && !fs.existsSync(localPackageJson)) {
throw new Error(
`
Make sure you have a local package.json file at
"${localPackageJson}".
Otherwise Jest will try to traverse the directory tree and find the
global package.json, which will send Jest into infinite loop.
`,
);
}
const env = Object.assign({}, process.env, {FORCE_COLOR: '0'});
if (options.nodeOptions) env['NODE_OPTIONS'] = options.nodeOptions;
if (options.nodePath) env['NODE_PATH'] = options.nodePath;
const spawnArgs = [JEST_PATH, ...(args || [])];
const spawnOptions = {
cwd: dir,
env,
reject: false,
timeout: options.timeout || 0,
};
return (spawnAsync ? execa : execa.sync)(
process.execPath,
spawnArgs,
spawnOptions,
);
}
type RunJestResult = ExecaReturns & {
status?: number;
code?: number;
json?: (
dir: string,
args: Array<string> | undefined,
options: RunJestOptions,
) => RunJestResult;
};
function normalizeResult(result: RunJestResult, options: RunJestOptions) {
// For compat with cross-spawn
result.status = result.code;
result.stdout = normalizeIcons(result.stdout);
if (options.stripAnsi) result.stdout = stripAnsi(result.stdout);
result.stderr = normalizeIcons(result.stderr);
if (options.stripAnsi) result.stderr = stripAnsi(result.stderr);
return result;
}
// Runs `jest` with `--json` option and adds `json` property to the result obj.
// 'success', 'startTime', 'numTotalTests', 'numTotalTestSuites',
// 'numRuntimeErrorTestSuites', 'numPassedTests', 'numFailedTests',
// 'numPendingTests', 'testResults'
export const json = function(
dir: string,
args: Array<string> | undefined,
options: RunJestOptions = {},
): RunJestResult {
args = [...(args || []), '--json'];
const result = runJest(dir, args, options);
try {
result.json = JSON.parse(result.stdout || '');
} catch (e) {
throw new Error(
`
Can't parse JSON.
ERROR: ${e.name} ${e.message}
STDOUT: ${result.stdout}
STDERR: ${result.stderr}
`,
);
}
return result;
};
// Runs `jest` until a given output is achieved, then kills it with `SIGTERM`
export const until = async function(
dir: string,
args: Array<string> | undefined,
text: string,
options: RunJestOptions = {},
) {
const jestPromise = spawnJest(dir, args, {timeout: 30000, ...options}, true);
jestPromise.stderr.pipe(
new Writable({
write(chunk, _encoding, callback) {
const output = chunk.toString('utf8');
if (output.includes(text)) {
jestPromise.kill();
}
callback();
},
}),
);
return normalizeResult(await jestPromise, options);
};