-
Notifications
You must be signed in to change notification settings - Fork 454
/
preprocess.ts
79 lines (66 loc) · 2.54 KB
/
preprocess.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
import { flushLogs, logOnce } from './utils/logger';
import { postProcessCode } from './postprocess';
import { transpileTypescript } from './transpiler';
import runTsDiagnostics from './utils/run-ts-diagnostics';
import getTSConfig from './utils/get-ts-config';
import getTSJestConfig from './utils/get-ts-jest-config';
export default function preprocess(
src: string,
filePath: jest.Path,
jestConfig: jest.ProjectConfig,
transformOptions?: jest.TransformOptions,
): jest.TransformedSource | string {
// transformOptions.instrument is a proxy for collectCoverage
// https://github.com/kulshekhar/ts-jest/issues/201#issuecomment-300572902
const compilerOptions = getTSConfig(jestConfig);
logOnce('final compilerOptions:', compilerOptions);
const isTsFile = /\.tsx?$/.test(filePath);
const isJsFile = /\.jsx?$/.test(filePath);
const isHtmlFile = /\.html$/.test(filePath);
// This is to support angular 2. See https://github.com/kulshekhar/ts-jest/pull/145
if (
isHtmlFile &&
jestConfig.globals &&
(jestConfig.globals as any).__TRANSFORM_HTML__
) {
src = 'module.exports=' + JSON.stringify(src) + ';';
}
const processFile =
compilerOptions.allowJs === true ? isTsFile || isJsFile : isTsFile;
if (!processFile) {
return src;
}
const tsJestConfig = getTSJestConfig(jestConfig);
logOnce('tsJestConfig: ', tsJestConfig);
// We can potentially do this faster by using the language service.
// See https://github.com/TypeStrong/ts-node/blob/master/src/index.ts#L268
if (
tsJestConfig.enableTsDiagnostics === true ||
(typeof tsJestConfig.enableTsDiagnostics === 'string' &&
new RegExp(tsJestConfig.enableTsDiagnostics).test(filePath))
) {
runTsDiagnostics(filePath, compilerOptions);
}
const transpileOutput = transpileTypescript(filePath, src, compilerOptions);
// FIXME: this should be done in the typescript source, else it's invalidating source maps
if (tsJestConfig.ignoreCoverageForAllDecorators === true) {
transpileOutput.code = transpileOutput.code.replace(
/\b__decorate\b/g,
'/* istanbul ignore next */__decorate',
);
}
if (tsJestConfig.ignoreCoverageForDecorators === true) {
transpileOutput.code = transpileOutput.code.replace(
/(\b__decorate\(\[\r?\n[^\n\r]*)\/\*\s*istanbul\s+ignore\s+decorator(.*)\*\//g,
'/* istanbul ignore next$2*/$1',
);
}
const outputText = postProcessCode(
jestConfig,
transformOptions,
transpileOutput,
filePath,
);
flushLogs();
return { code: outputText.code, map: outputText.map };
}