forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
150 lines (131 loc) · 3.87 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
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
/**
* 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 * as path from 'path';
import type {Config} from '@jest/types';
import type {AggregatedResult} from '@jest/test-result';
import {clearLine, tryRealpath} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import {deprecationEntries} from 'jest-config';
import {getVersion, runCLI} from '@jest/core';
import chalk = require('chalk');
import exit = require('exit');
import yargs = require('yargs');
import init from '../init';
import * as args from './args';
export async function run(
maybeArgv?: Array<string>,
project?: Config.Path,
): Promise<void> {
try {
const argv: Config.Argv = 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) {
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 const buildArgv = (maybeArgv?: Array<string>): Config.Argv => {
const version =
getVersion() +
(__dirname.includes(`packages${path.sep}jest-cli`) ? '-dev' : '');
const rawArgv: Config.Argv | Array<string> =
maybeArgv || process.argv.slice(2);
const argv: Config.Argv = 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?: Config.Path,
) => {
const projects = argv.projects ? argv.projects : [];
if (project) {
projects.push(project);
}
if (!projects.length && process.platform === 'win32') {
try {
projects.push(tryRealpath(process.cwd()));
} catch (err) {
// 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) {
setTimeout(() => {
console.warn(
chalk.yellow.bold(
'Jest did not exit one second 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.',
),
);
}, 1000).unref();
}
};