/
jest-preprocessor.ts
147 lines (122 loc) · 3.85 KB
/
jest-preprocessor.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
import type { TranspileOptions, Diagnostic } from '@stencil/core/internal';
import { loadTypeScriptDiagnostic, normalizePath } from '@utils';
import { transpile } from '../test-transpile';
import { ts } from '@stencil/core/compiler';
export const jestPreprocessor = {
process(sourceText: string, filePath: string, jestConfig: { rootDir: string }) {
if (shouldTransform(filePath, sourceText)) {
const opts: TranspileOptions = {
file: filePath,
currentDirectory: jestConfig.rootDir,
};
const tsCompilerOptions: ts.CompilerOptions = this.getCompilerOptions(jestConfig.rootDir);
if (tsCompilerOptions) {
if (tsCompilerOptions.baseUrl) {
opts.baseUrl = tsCompilerOptions.baseUrl;
}
if (tsCompilerOptions.paths) {
opts.paths = tsCompilerOptions.paths;
}
}
const results = transpile(sourceText, opts);
const hasErrors = results.diagnostics.some(diagnostic => diagnostic.level === 'error');
if (results.diagnostics && hasErrors) {
const msg = results.diagnostics.map(formatDiagnostic).join('\n\n');
throw new Error(msg);
}
return results.code;
}
return sourceText;
},
getCompilerOptions(rootDir: string) {
if (!this._tsCompilerOptions) {
this._tsCompilerOptions = getCompilerOptions(rootDir);
}
return this._tsCompilerOptions;
},
getCacheKey(
code: string,
filePath: string,
jestConfigStr: string,
transformOptions: { instrument: boolean; rootDir: string },
) {
// https://github.com/facebook/jest/blob/v23.6.0/packages/jest-runtime/src/script_transformer.js#L61-L90
if (!this._tsCompilerOptionsKey) {
const opts = this.getCompilerOptions(transformOptions.rootDir);
this._tsCompilerOptionsKey = JSON.stringify(opts);
}
const key = [
process.version,
this._tsCompilerOptionsKey,
code,
filePath,
jestConfigStr,
!!transformOptions.instrument,
6, // cache buster
];
return key.join(':');
},
};
function formatDiagnostic(diagnostic: Diagnostic) {
let m = '';
if (diagnostic.relFilePath) {
m += diagnostic.relFilePath;
if (typeof diagnostic.lineNumber === 'number') {
m += ':' + diagnostic.lineNumber + 1;
if (typeof diagnostic.columnNumber === 'number') {
m += ':' + diagnostic.columnNumber;
}
}
m += '\n';
}
m += diagnostic.messageText;
return m;
}
function getCompilerOptions(rootDir: string) {
if (typeof rootDir !== 'string') {
return null;
}
rootDir = normalizePath(rootDir);
const tsconfigFilePath = ts.findConfigFile(rootDir, ts.sys.fileExists);
if (!tsconfigFilePath) {
return null;
}
const tsconfigResults = ts.readConfigFile(tsconfigFilePath, ts.sys.readFile);
if (tsconfigResults.error) {
throw new Error(formatDiagnostic(loadTypeScriptDiagnostic(tsconfigResults.error)));
}
const parseResult = ts.parseJsonConfigFileContent(
tsconfigResults.config,
ts.sys,
rootDir,
undefined,
tsconfigFilePath,
);
return parseResult.options;
}
export function shouldTransform(filePath: string, sourceText: string) {
const ext = filePath.split('.').pop().toLowerCase().split('?')[0];
if (ext === 'ts' || ext === 'tsx' || ext === 'jsx') {
// typescript extensions (to include .d.ts)
return true;
}
if (ext === 'mjs') {
// es module extensions
return true;
}
if (ext === 'js') {
// there may be false positives here
// but worst case scenario a commonjs file is transpiled to commonjs
if (sourceText.includes('import ') || sourceText.includes('import.') || sourceText.includes('import(')) {
return true;
}
if (sourceText.includes('export ')) {
return true;
}
}
if (ext === 'css') {
// convert a standard css file into an nodejs ready file
return true;
}
return false;
}