forked from jestjs/jest
/
index.ts
170 lines (153 loc) · 4.87 KB
/
index.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {createHash} from 'crypto';
import * as path from 'path';
import {
PartialConfig,
TransformOptions,
transformSync as babelTransform,
} from '@babel/core';
import chalk = require('chalk');
import * as fs from 'graceful-fs';
import slash = require('slash');
import type {
TransformOptions as JestTransformOptions,
SyncTransformer,
} from '@jest/transform';
import type {Config} from '@jest/types';
import {loadPartialConfig} from './loadBabelConfig';
const THIS_FILE = fs.readFileSync(__filename);
const jestPresetPath = require.resolve('babel-preset-jest');
const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul');
type CreateTransformer = SyncTransformer<TransformOptions>['createTransformer'];
const createTransformer: CreateTransformer = userOptions => {
const inputOptions = userOptions ?? {};
const options = {
...inputOptions,
caller: {
name: 'babel-jest',
supportsDynamicImport: false,
supportsExportNamespaceFrom: false,
supportsStaticESM: false,
supportsTopLevelAwait: false,
...inputOptions.caller,
},
compact: false,
plugins: inputOptions.plugins ?? [],
presets: (inputOptions.presets ?? []).concat(jestPresetPath),
sourceMaps: 'both',
} as const;
function loadBabelConfig(
cwd: Config.Path,
filename: Config.Path,
transformOptions: JestTransformOptions,
): PartialConfig {
// `cwd` first to allow incoming options to override it
const babelConfig = loadPartialConfig({
cwd,
...options,
caller: {
...options.caller,
supportsDynamicImport:
transformOptions.supportsDynamicImport ??
options.caller.supportsDynamicImport,
supportsExportNamespaceFrom:
transformOptions.supportsExportNamespaceFrom ??
options.caller.supportsExportNamespaceFrom,
supportsStaticESM:
transformOptions.supportsStaticESM ??
options.caller.supportsStaticESM,
supportsTopLevelAwait:
transformOptions.supportsTopLevelAwait ??
options.caller.supportsTopLevelAwait,
},
filename,
});
if (!babelConfig) {
throw new Error(
`babel-jest: Babel ignores ${chalk.bold(
slash(path.relative(cwd, filename)),
)} - make sure to include the file in Jest's ${chalk.bold(
'transformIgnorePatterns',
)} as well.`,
);
}
return babelConfig;
}
return {
canInstrument: true,
getCacheKey(sourceText, sourcePath, transformOptions) {
const {config, configString, instrument} = transformOptions;
const babelOptions = loadBabelConfig(
config.cwd,
sourcePath,
transformOptions,
);
const configPath = [
babelOptions.config || '',
babelOptions.babelrc || '',
];
return createHash('md5')
.update(THIS_FILE)
.update('\0', 'utf8')
.update(JSON.stringify(babelOptions.options))
.update('\0', 'utf8')
.update(sourceText)
.update('\0', 'utf8')
.update(path.relative(config.rootDir, sourcePath))
.update('\0', 'utf8')
.update(configString)
.update('\0', 'utf8')
.update(configPath.join(''))
.update('\0', 'utf8')
.update(instrument ? 'instrument' : '')
.update('\0', 'utf8')
.update(process.env.NODE_ENV || '')
.update('\0', 'utf8')
.update(process.env.BABEL_ENV || '')
.digest('hex');
},
process(sourceText, sourcePath, transformOptions) {
const babelOptions = {
...loadBabelConfig(
transformOptions.config.cwd,
sourcePath,
transformOptions,
).options,
};
if (transformOptions?.instrument) {
babelOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
// Copied from jest-runtime transform.js
babelOptions.plugins = (babelOptions.plugins || []).concat([
[
babelIstanbulPlugin,
{
// files outside `cwd` will not be instrumented
cwd: transformOptions.config.rootDir,
exclude: [],
},
],
]);
}
const transformResult = babelTransform(sourceText, babelOptions);
if (transformResult) {
const {code, map} = transformResult;
if (typeof code === 'string') {
return {code, map};
}
}
return sourceText;
},
};
};
const transformer: SyncTransformer<TransformOptions> = {
...createTransformer(),
// Assigned here so only the exported transformer has `createTransformer`,
// instead of all created transformers by the function
createTransformer,
};
export = transformer;