/
postprocess.ts
80 lines (74 loc) · 2.42 KB
/
postprocess.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
/**
* Postprocess step. Based on babel-jest: https://github.com/facebook/jest/blob/master/packages/babel-jest/src/index.js
* https://github.com/facebook/jest/blob/9b157c3a7c325c3971b2aabbe4c8ab4ce0b0c56d/packages/babel-jest/src/index.js
*/
import * as babel from 'babel-core';
import istanbulPlugin from 'babel-plugin-istanbul';
import * as jestPreset from 'babel-preset-jest';
import { CompilerOptions } from 'typescript/lib/typescript';
import {
BabelTransformOptions,
FullJestConfig,
JestConfig,
PostProcessHook,
TransformOptions,
TsJestConfig,
} from './jest-types';
function createBabelTransformer(options: BabelTransformOptions) {
options = {
...options,
plugins: options.plugins || [],
presets: (options.presets || []).concat([jestPreset]),
// If retainLines isn't set to true, the line numbers
// are off by 1
retainLines: true,
// force the sourceMaps property to be 'inline' during testing
// to help generate accurate sourcemaps.
sourceMaps: 'inline',
};
delete options.cacheDirectory;
delete options.filename;
return (
src: string,
filename: string,
config: JestConfig,
transformOptions: TransformOptions,
): string => {
const theseOptions = Object.assign({ filename }, options);
if (transformOptions && transformOptions.instrument) {
theseOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
// Copied from jest-runtime transform.js
theseOptions.plugins = theseOptions.plugins.concat([
[
istanbulPlugin,
{
// files outside `cwd` will not be instrumented
cwd: config.rootDir,
exclude: [],
},
],
]);
}
return babel.transform(src, theseOptions).code;
};
}
export const getPostProcessHook = (
tsCompilerOptions: CompilerOptions,
jestConfig: JestConfig,
tsJestConfig: TsJestConfig,
): PostProcessHook => {
if (tsJestConfig.skipBabel) {
return src => src; // Identity function
}
const plugins = Array.from(tsJestConfig.babelConfig && tsJestConfig.babelConfig.plugins || []);
// If we're not skipping babel
if (tsCompilerOptions.allowSyntheticDefaultImports) {
plugins.push('transform-es2015-modules-commonjs');
}
return createBabelTransformer({
...tsJestConfig.babelConfig,
babelrc: tsJestConfig.useBabelrc || false,
plugins,
presets: tsJestConfig.babelConfig ? tsJestConfig.babelConfig.presets : [],
});
};