forked from nrwl/nx
/
jest.impl.ts
152 lines (135 loc) · 3.95 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
import {
BuilderContext,
createBuilder,
BuilderOutput
} from '@angular-devkit/architect';
import { from, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import * as path from 'path';
import { JsonObject } from '@angular-devkit/core';
try {
require('dotenv').config();
} catch (e) {}
import { runCLI } from 'jest';
if (process.env.NODE_ENV == null || process.env.NODE_ENV == undefined) {
(process.env as any).NODE_ENV = 'test';
}
export interface JestBuilderOptions extends JsonObject {
codeCoverage?: boolean;
config?: string;
jestConfig: string;
testFile?: string;
setupFile?: string;
tsConfig: string;
bail?: number;
ci?: boolean;
color?: boolean;
clearCache?: boolean;
findRelatedTests?: string;
json?: boolean;
maxWorkers?: number;
onlyChanged?: boolean;
outputFile?: string;
passWithNoTests?: boolean;
runInBand?: boolean;
silent?: boolean;
testNamePattern?: string;
testPathPattern?: string[];
colors?: boolean;
reporters?: string[];
verbose?: boolean;
coverage?: boolean;
coverageReporters?: string;
coverageDirectory?: string;
testResultsProcessor?: string;
updateSnapshot?: boolean;
useStderr?: boolean;
watch?: boolean;
watchAll?: boolean;
testLocationInResults?: boolean;
}
export default createBuilder<JestBuilderOptions>(run);
function run(
options: JestBuilderOptions,
context: BuilderContext
): Observable<BuilderOutput> {
options.jestConfig = path.resolve(context.workspaceRoot, options.jestConfig);
const tsJestConfig = {
tsConfig: path.resolve(context.workspaceRoot, options.tsConfig)
};
// TODO: This is hacky, We should probably just configure it in the user's workspace
// If jest-preset-angular is installed, apply settings
try {
require.resolve('jest-preset-angular');
Object.assign(tsJestConfig, {
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer'
]
});
} catch (e) {}
// merge the jestConfig globals with our 'ts-jest' override
const jestConfig: { globals: any } = require(options.jestConfig);
const globals = jestConfig.globals || {};
Object.assign(globals, {
'ts-jest': {
...(globals['ts-jest'] || {}),
...tsJestConfig
}
});
const config: any = {
_: [],
config: options.config,
coverage: options.codeCoverage,
bail: options.bail,
ci: options.ci,
color: options.color,
json: options.json,
maxWorkers: options.maxWorkers,
onlyChanged: options.onlyChanged,
outputFile: options.outputFile,
passWithNoTests: options.passWithNoTests,
runInBand: options.runInBand,
silent: options.silent,
testLocationInResults: options.testLocationInResults,
testNamePattern: options.testNamePattern,
testPathPattern: options.testPathPattern,
colors: options.colors,
verbose: options.verbose,
coverageReporters: options.coverageReporters,
coverageDirectory: options.coverageDirectory,
testResultsProcessor: options.testResultsProcessor,
updateSnapshot: options.updateSnapshot,
useStderr: options.useStderr,
watch: options.watch,
watchAll: options.watchAll,
globals: JSON.stringify(globals)
};
if (options.setupFile) {
config.setupFilesAfterEnv = [
path.resolve(context.workspaceRoot, options.setupFile)
];
}
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.clearCache) {
config.clearCache = true;
}
if (options.reporters && options.reporters.length > 0) {
config.reporters = options.reporters;
}
return from(runCLI(config, [options.jestConfig])).pipe(
map(results => {
return {
success: results.results.success
};
})
);
}