Skip to content

Commit 49204ac

Browse files
authored
fix(jest-transform): ensure correct config is passed to preprocessors specified multiple times in transform (#13770)
1 parent 928f6cc commit 49204ac

File tree

3 files changed

+78
-15
lines changed

3 files changed

+78
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- `[jest-runtime]` Support Wasm files that import JS resources ([#13608](https://github.com/facebook/jest/pull/13608))
2121
- `[jest-runtime]` Using the scriptTransformer cache in jest-runner ([#13735](https://github.com/facebook/jest/pull/13735))
2222
- `[jest-snapshot]` Make sure to import `babel` outside of the sandbox ([#13694](https://github.com/facebook/jest/pull/13694))
23+
- `[jest-transform]` Ensure the correct configuration is passed to preprocessors specified multiple times in the `transform` option ([#13770](https://github.com/facebook/jest/pull/13770))
2324

2425
### Chore & Maintenance
2526

packages/jest-transform/src/ScriptTransformer.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ class ScriptTransformer {
134134
.substring(0, 32);
135135
}
136136

137+
private _buildTransformCacheKey(pattern: string, filepath: string) {
138+
return pattern + filepath;
139+
}
140+
137141
private _getCacheKey(
138142
fileData: string,
139143
filename: string,
@@ -243,25 +247,35 @@ class ScriptTransformer {
243247
return this._createCachedFilename(filename, cacheKey);
244248
}
245249

246-
private _getTransformPath(filename: string) {
247-
const transformRegExp = this._cache.transformRegExp;
248-
if (!transformRegExp) {
250+
private _getTransformPatternAndPath(filename: string) {
251+
const transformEntry = this._cache.transformRegExp;
252+
if (transformEntry == null) {
249253
return undefined;
250254
}
251255

252-
for (let i = 0; i < transformRegExp.length; i++) {
253-
if (transformRegExp[i][0].test(filename)) {
254-
return transformRegExp[i][1];
256+
for (let i = 0; i < transformEntry.length; i++) {
257+
const [transformRegExp, transformPath] = transformEntry[i];
258+
if (transformRegExp.test(filename)) {
259+
return [transformRegExp.source, transformPath];
255260
}
256261
}
257262

258263
return undefined;
259264
}
260265

266+
private _getTransformPath(filename: string) {
267+
const transformInfo = this._getTransformPatternAndPath(filename);
268+
if (!Array.isArray(transformInfo)) {
269+
return undefined;
270+
}
271+
272+
return transformInfo[1];
273+
}
274+
261275
async loadTransformers(): Promise<void> {
262276
await Promise.all(
263277
this._config.transform.map(
264-
async ([, transformPath, transformerConfig]) => {
278+
async ([transformPattern, transformPath, transformerConfig], i) => {
265279
let transformer: Transformer | TransformerFactory<Transformer> =
266280
await requireOrImportModule(transformPath);
267281

@@ -278,7 +292,12 @@ class ScriptTransformer {
278292
throw new Error(makeInvalidTransformerError(transformPath));
279293
}
280294
const res = {transformer, transformerConfig};
281-
this._transformCache.set(transformPath, res);
295+
const transformCacheKey = this._buildTransformCacheKey(
296+
this._cache.transformRegExp?.[i]?.[0].source ??
297+
new RegExp(transformPattern).source,
298+
transformPath,
299+
);
300+
this._transformCache.set(transformCacheKey, res);
282301
},
283302
),
284303
);
@@ -297,15 +316,19 @@ class ScriptTransformer {
297316
return null;
298317
}
299318

300-
const transformPath = this._getTransformPath(filename);
301-
302-
if (transformPath == null) {
319+
const transformPatternAndPath = this._getTransformPatternAndPath(filename);
320+
if (!Array.isArray(transformPatternAndPath)) {
303321
return null;
304322
}
305323

306-
const cached = this._transformCache.get(transformPath);
307-
if (cached != null) {
308-
return cached;
324+
const [transformPattern, transformPath] = transformPatternAndPath;
325+
const transformCacheKey = this._buildTransformCacheKey(
326+
transformPattern,
327+
transformPath,
328+
);
329+
const transformer = this._transformCache.get(transformCacheKey);
330+
if (transformer !== undefined) {
331+
return transformer;
309332
}
310333

311334
throw new Error(

packages/jest-transform/src/__tests__/ScriptTransformer.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,45 @@ describe('ScriptTransformer', () => {
15581558
).toHaveBeenCalledWith(transformerConfig);
15591559
});
15601560

1561+
it('passes correct config to a preprocessor used multiple times', async () => {
1562+
const transformerConfig1 = {};
1563+
const transformerConfig2 = {};
1564+
1565+
config = Object.assign(config, {
1566+
transform: [
1567+
// same preprocessor
1568+
[
1569+
// *only* /fruits/banana.js
1570+
'/fruits/banana\\.js$',
1571+
'configureable-preprocessor',
1572+
transformerConfig1,
1573+
],
1574+
[
1575+
// *not* /fruits/banana.js
1576+
'/fruits/(?!banana)\\w+\\.js$',
1577+
'configureable-preprocessor',
1578+
transformerConfig2,
1579+
],
1580+
],
1581+
});
1582+
1583+
const scriptTransformer = await createScriptTransformer(config);
1584+
1585+
scriptTransformer.transform('/fruits/banana.js', getCoverageOptions());
1586+
expect(
1587+
(
1588+
require('configureable-preprocessor') as TransformerFactory<SyncTransformer>
1589+
).createTransformer,
1590+
).toHaveBeenLastCalledWith(transformerConfig1);
1591+
1592+
scriptTransformer.transform('/fruits/kiwi.js', getCoverageOptions());
1593+
expect(
1594+
(
1595+
require('configureable-preprocessor') as TransformerFactory<SyncTransformer>
1596+
).createTransformer,
1597+
).toHaveBeenLastCalledWith(transformerConfig2);
1598+
});
1599+
15611600
it('reads values from the cache', async () => {
15621601
const transformConfig: Config.ProjectConfig = {
15631602
...config,
@@ -2001,7 +2040,7 @@ describe('ScriptTransformer', () => {
20012040

20022041
// @ts-expect-error - private property
20032042
expect(Array.from(scriptTransformer._transformCache.entries())).toEqual([
2004-
['test_preprocessor', expect.any(Object)],
2043+
['\\.js$test_preprocessor', expect.any(Object)],
20052044
]);
20062045
});
20072046
});

0 commit comments

Comments
 (0)