/
run.ts
155 lines (136 loc) · 4.03 KB
/
run.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
/**
* 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 * as path from 'path';
import chalk = require('chalk');
import exit = require('exit');
import yargs = require('yargs');
import {getVersion, runCLI} from '@jest/core';
import type {AggregatedResult} from '@jest/test-result';
import type {Config} from '@jest/types';
import {deprecationEntries} from 'jest-config';
import {clearLine, tryRealpath} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import * as args from './args';
import init from './init';
export async function run(
maybeArgv?: Array<string>,
project?: string,
): Promise<void> {
try {
const argv = await buildArgv(maybeArgv);
if (argv.init) {
await init();
return;
}
const projects = getProjectListFromCLIArgs(argv, project);
const {results, globalConfig} = await runCLI(argv, projects);
readResultsAndExit(results, globalConfig);
} catch (error: any) {
clearLine(process.stderr);
clearLine(process.stdout);
if (error?.stack) {
console.error(chalk.red(error.stack));
} else {
console.error(chalk.red(error));
}
exit(1);
throw error;
}
}
export async function buildArgv(
maybeArgv?: Array<string>,
): Promise<Config.Argv> {
const version =
getVersion() +
(__dirname.includes(`packages${path.sep}jest-cli`) ? '-dev' : '');
const rawArgv: Array<string> = maybeArgv || process.argv.slice(2);
const argv: Config.Argv = await yargs(rawArgv)
.usage(args.usage)
.version(version)
.alias('help', 'h')
.options(args.options)
.epilogue(args.docs)
.check(args.check).argv;
validateCLIOptions(
argv,
{...args.options, deprecationEntries},
// strip leading dashes
Array.isArray(rawArgv)
? rawArgv.map(rawArgv => rawArgv.replace(/^--?/, ''))
: Object.keys(rawArgv),
);
// strip dashed args
return Object.keys(argv).reduce<Config.Argv>(
(result, key) => {
if (!key.includes('-')) {
result[key] = argv[key];
}
return result;
},
{$0: argv.$0, _: argv._},
);
}
const getProjectListFromCLIArgs = (argv: Config.Argv, project?: string) => {
const projects = argv.projects ? argv.projects : [];
if (project) {
projects.push(project);
}
if (!projects.length && process.platform === 'win32') {
try {
projects.push(tryRealpath(process.cwd()));
} catch {
// do nothing, just catch error
// process.binding('fs').realpath can throw, e.g. on mapped drives
}
}
if (!projects.length) {
projects.push(process.cwd());
}
return projects;
};
const readResultsAndExit = (
result: AggregatedResult | null,
globalConfig: Config.GlobalConfig,
) => {
const code = !result || result.success ? 0 : globalConfig.testFailureExitCode;
// Only exit if needed
process.on('exit', () => {
if (typeof code === 'number' && code !== 0) {
process.exitCode = code;
}
});
if (globalConfig.forceExit) {
if (!globalConfig.detectOpenHandles) {
console.warn(
`${chalk.bold(
'Force exiting Jest: ',
)}Have you considered using \`--detectOpenHandles\` to detect ` +
'async operations that kept running after all tests finished?',
);
}
exit(code);
} else if (
!globalConfig.detectOpenHandles &&
globalConfig.openHandlesTimeout !== 0
) {
const timeout = globalConfig.openHandlesTimeout;
setTimeout(() => {
console.warn(
chalk.yellow.bold(
`Jest did not exit ${
timeout === 1000 ? 'one second' : `${timeout / 1000} seconds`
} after the test run has completed.\n\n'`,
) +
chalk.yellow(
'This usually means that there are asynchronous operations that ' +
"weren't stopped in your tests. Consider running Jest with " +
'`--detectOpenHandles` to troubleshoot this issue.',
),
);
}, timeout).unref();
}
};