Skip to content

Commit

Permalink
fix(runtime): handle async transforms of same module
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Mar 20, 2021
1 parent 420bcb7 commit 358d0d6
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 28 deletions.
2 changes: 1 addition & 1 deletion e2e/__tests__/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ onNodeVersions('^12.17.0 || >=13.2.0', () => {
});
expect(stderr).toMatch(/PASS/);
expect(json.success).toBe(true);
expect(json.numPassedTests).toBe(1);
expect(json.numPassedTests).toBe(2);
});
});

Expand Down
7 changes: 6 additions & 1 deletion e2e/transform/async-transformer/__tests__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

import m from '../module-under-test';
import m, {exportedSymbol} from '../module-under-test';
import symbol from '../some-symbol';

test('ESM transformer intercepts', () => {
expect(m).toEqual(42);
});

test('reexported symbol is same instance', () => {
expect(exportedSymbol).toBe(symbol);
});
4 changes: 4 additions & 0 deletions e2e/transform/async-transformer/module-under-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import symbol from './some-symbol';

export const exportedSymbol = symbol;

export default 'It was not transformed!!';
19 changes: 17 additions & 2 deletions e2e/transform/async-transformer/my-transform.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,29 @@

'use strict';

const {promisify} = require('util');

const wait = promisify(setTimeout);

const fileToTransform = require.resolve('./module-under-test');
const fileToTransform2 = require.resolve('./some-symbol');

module.exports = {
async processAsync(src, filepath) {
if (filepath !== fileToTransform) {
if (filepath !== fileToTransform && filepath !== fileToTransform2) {
throw new Error(`Unsupported filepath ${filepath}`);
}

return 'export default 42;';
if (filepath === fileToTransform2) {
// we want to wait to ensure the module cache is populated with the correct module
await wait(100);

return src;
}

return src.replace(
"export default 'It was not transformed!!'",
'export default 42',
);
},
};
3 changes: 2 additions & 1 deletion e2e/transform/async-transformer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"jest": {
"testEnvironment": "node",
"transform": {
"module-under-test\\.js$": "<rootDir>/my-transform.cjs"
"module-under-test\\.js$": "<rootDir>/my-transform.cjs",
"some-symbol\\.js$": "<rootDir>/my-transform.cjs"
}
}
}
8 changes: 8 additions & 0 deletions e2e/transform/async-transformer/some-symbol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 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.
*/

export default Symbol('hello!');
84 changes: 61 additions & 23 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export default class Runtime {
private readonly _sourceMapRegistry: Map<string, string>;
private readonly _scriptTransformer: ScriptTransformer;
private readonly _fileTransforms: Map<string, RuntimeTransformResult>;
private readonly _fileTransformsMutex: Map<string, Promise<void>>;
private _v8CoverageInstrumenter: CoverageInstrumenter | undefined;
private _v8CoverageResult: V8Coverage | undefined;
private readonly _transitiveShouldMock: Map<string, boolean>;
Expand Down Expand Up @@ -232,6 +233,7 @@ export default class Runtime {
this._shouldAutoMock = config.automock;
this._sourceMapRegistry = new Map();
this._fileTransforms = new Map();
this._fileTransformsMutex = new Map();
this._virtualMocks = new Map();
this.jestObjectCaches = new Map();

Expand Down Expand Up @@ -374,6 +376,10 @@ export default class Runtime {
): Promise<VMModule> {
const cacheKey = modulePath + query;

if (this._fileTransformsMutex.has(cacheKey)) {
await this._fileTransformsMutex.get(cacheKey);
}

if (!this._esmoduleRegistry.has(cacheKey)) {
invariant(
typeof this._environment.getVmContext === 'function',
Expand All @@ -384,9 +390,28 @@ export default class Runtime {

invariant(context, 'Test environment has been torn down');

let transformResolve: () => void;
let transformReject: (error?: unknown) => void;

this._fileTransformsMutex.set(
cacheKey,
new Promise((resolve, reject) => {
transformResolve = resolve;
transformReject = reject;
}),
);

invariant(
transformResolve! && transformReject!,
'Promise initialization should be sync - please report this bug to Jest!',
);

if (this._resolver.isCoreModule(modulePath)) {
const core = this._importCoreModule(modulePath, context);
this._esmoduleRegistry.set(cacheKey, core);

transformResolve();

return core;
}

Expand All @@ -398,31 +423,43 @@ export default class Runtime {
supportsTopLevelAwait,
});

const module = new SourceTextModule(transformedCode, {
context,
identifier: modulePath,
importModuleDynamically: async (
specifier: string,
referencingModule: VMModule,
) => {
invariant(
runtimeSupportsVmModules,
'You need to run with a version of node that supports ES Modules in the VM API. See https://jestjs.io/docs/en/ecmascript-modules',
);
const module = await this.resolveModule(
specifier,
referencingModule.identifier,
referencingModule.context,
);
try {
const module = new SourceTextModule(transformedCode, {
context,
identifier: modulePath,
importModuleDynamically: async (
specifier: string,
referencingModule: VMModule,
) => {
invariant(
runtimeSupportsVmModules,
'You need to run with a version of node that supports ES Modules in the VM API. See https://jestjs.io/docs/en/ecmascript-modules',
);
const module = await this.resolveModule(
specifier,
referencingModule.identifier,
referencingModule.context,
);

return this.linkAndEvaluateModule(module);
},
initializeImportMeta(meta: ImportMeta) {
meta.url = pathToFileURL(modulePath).href;
},
});

return this.linkAndEvaluateModule(module);
},
initializeImportMeta(meta: ImportMeta) {
meta.url = pathToFileURL(modulePath).href;
},
});
invariant(
!this._esmoduleRegistry.has(cacheKey),
`Module cache already has entry ${cacheKey}. This is a bug in Jest, please report it!`,
);

this._esmoduleRegistry.set(cacheKey, module);
this._esmoduleRegistry.set(cacheKey, module);

transformResolve();
} catch (error: unknown) {
transformReject(error);
throw error;
}
}

const module = this._esmoduleRegistry.get(cacheKey);
Expand Down Expand Up @@ -990,6 +1027,7 @@ export default class Runtime {
this._sourceMapRegistry.clear();

this._fileTransforms.clear();
this._fileTransformsMutex.clear();
this.jestObjectCaches.clear();

this._v8CoverageResult = [];
Expand Down

0 comments on commit 358d0d6

Please sign in to comment.