/
jest.impl.ts
177 lines (151 loc) · 4.97 KB
/
jest.impl.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
167
168
169
170
171
172
173
174
175
176
177
import 'dotenv/config';
import { runCLI } from 'jest';
import { readConfig } from 'jest-config';
import { utils as jestReporterUtils } from '@jest/reporters';
import { makeEmptyAggregatedTestResult, addResult } from '@jest/test-result';
import * as path from 'path';
import { JestExecutorOptions } from './schema';
import { Config } from '@jest/types';
import { ExecutorContext, TaskGraph } from '@nrwl/devkit';
import { join } from 'path';
import { getSummary } from './summary';
process.env.NODE_ENV ??= 'test';
export async function jestExecutor(
options: JestExecutorOptions,
context: ExecutorContext
): Promise<{ success: boolean }> {
const config = await jestConfigParser(options, context);
const { results } = await runCLI(config, [options.jestConfig]);
return { success: results.success };
}
export async function jestConfigParser(
options: JestExecutorOptions,
context: ExecutorContext,
multiProjects = false
): Promise<Config.Argv> {
let jestConfig:
| {
transform: any;
globals: any;
setupFilesAfterEnv: any;
}
| undefined;
const config: Config.Argv = {
$0: undefined,
_: [],
config: options.config,
coverage: options.codeCoverage,
bail: options.bail,
ci: options.ci,
color: options.color,
detectOpenHandles: options.detectOpenHandles,
json: options.json,
maxWorkers: options.maxWorkers,
onlyChanged: options.onlyChanged,
changedSince: options.changedSince,
outputFile: options.outputFile,
passWithNoTests: options.passWithNoTests,
runInBand: options.runInBand,
showConfig: options.showConfig,
silent: options.silent,
testLocationInResults: options.testLocationInResults,
testNamePattern: options.testNamePattern,
testPathPattern: options.testPathPattern,
testTimeout: options.testTimeout,
colors: options.colors,
verbose: options.verbose,
testResultsProcessor: options.testResultsProcessor,
updateSnapshot: options.updateSnapshot,
useStderr: options.useStderr,
watch: options.watch,
watchAll: options.watchAll,
};
if (!multiProjects) {
options.jestConfig = path.resolve(context.root, options.jestConfig);
jestConfig = (await readConfig(config, options.jestConfig)).projectConfig;
}
// for backwards compatibility
if (options.setupFile && !multiProjects) {
const setupFilesAfterEnvSet = new Set([
...(jestConfig.setupFilesAfterEnv ?? []),
path.resolve(context.root, options.setupFile),
]);
config.setupFilesAfterEnv = Array.from(setupFilesAfterEnvSet);
}
if (options.testFile) {
config._.push(options.testFile);
}
if (options.findRelatedTests) {
const parsedTests = options.findRelatedTests
.split(',')
.map((s) => s.trim());
config._.push(...parsedTests);
config.findRelatedTests = true;
}
if (options.coverageDirectory) {
config.coverageDirectory = path.join(
context.root,
options.coverageDirectory
);
}
if (options.clearCache) {
config.clearCache = true;
}
if (options.reporters && options.reporters.length > 0) {
config.reporters = options.reporters;
}
if (
Array.isArray(options.coverageReporters) &&
options.coverageReporters.length > 0
) {
config.coverageReporters = options.coverageReporters;
}
return config;
}
export default jestExecutor;
export async function batchJest(
taskGraph: TaskGraph,
inputs: Record<string, JestExecutorOptions>,
overrides: JestExecutorOptions,
context: ExecutorContext
): Promise<Record<string, { success: boolean; terminalOutput: string }>> {
const configPaths = taskGraph.roots.map((root) =>
path.resolve(context.root, inputs[root].jestConfig)
);
const { globalConfig, results } = await runCLI(
await jestConfigParser(overrides, context, true),
[...configPaths]
);
const jestTaskExecutionResults: Record<
string,
{ success: boolean; terminalOutput: string }
> = {};
const configs = await Promise.all(
configPaths.map(async (path) => readConfig({ $0: '', _: undefined }, path))
);
for (let i = 0; i < taskGraph.roots.length; i++) {
let root = taskGraph.roots[i];
const aggregatedResults = makeEmptyAggregatedTestResult();
aggregatedResults.startTime = results.startTime;
const projectRoot = join(context.root, taskGraph.tasks[root].projectRoot);
let resultOutput = '';
for (const testResult of results.testResults) {
if (testResult.testFilePath.startsWith(projectRoot)) {
addResult(aggregatedResults, testResult);
resultOutput +=
'\n\r' +
jestReporterUtils.getResultHeader(
testResult,
globalConfig,
configs[i].projectConfig
);
}
}
aggregatedResults.numTotalTestSuites = aggregatedResults.testResults.length;
jestTaskExecutionResults[root] = {
success: aggregatedResults.numFailedTests === 0,
terminalOutput: resultOutput + '\n\r\n\r' + getSummary(aggregatedResults),
};
}
return jestTaskExecutionResults;
}