Skip to content

Commit

Permalink
add 'preserveLoadOrder' flag and e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
znewsham committed Apr 19, 2024
1 parent 05c1a93 commit f079261
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 11 deletions.
4 changes: 4 additions & 0 deletions e2e/esm-load-order/__tests__/test.js
@@ -0,0 +1,4 @@
import '../esm-package';
import '../cjs-package';

it('load order is preserved', () => expect(Registrar['esm-package']).toEqual({}));

Check failure on line 4 in e2e/esm-load-order/__tests__/test.js

View workflow job for this annotation

GitHub Actions / Lint

Insert `⏎·`

Check failure on line 4 in e2e/esm-load-order/__tests__/test.js

View workflow job for this annotation

GitHub Actions / Lint

'Registrar' is not defined
1 change: 1 addition & 0 deletions e2e/esm-load-order/cjs-package/index.cjs
@@ -0,0 +1 @@
require('../esm-package/index.cjs');
4 changes: 4 additions & 0 deletions e2e/esm-load-order/cjs-package/package.json
@@ -0,0 +1,4 @@
{
"type": "commonjs",
"main": "./index.cjs"
}
1 change: 1 addition & 0 deletions e2e/esm-load-order/esm-package/index.cjs
@@ -0,0 +1 @@
module.exports = Registrar['esm-package'];

Check failure on line 1 in e2e/esm-load-order/esm-package/index.cjs

View workflow job for this annotation

GitHub Actions / Lint

'Registrar' is not defined
3 changes: 3 additions & 0 deletions e2e/esm-load-order/esm-package/index.js
@@ -0,0 +1,3 @@
globalThis.Registrar = {};

Registrar['esm-package'] = {};

Check failure on line 3 in e2e/esm-load-order/esm-package/index.js

View workflow job for this annotation

GitHub Actions / Lint

'Registrar' is not defined
11 changes: 11 additions & 0 deletions e2e/esm-load-order/esm-package/package.json
@@ -0,0 +1,11 @@
{
"type": "module",
"exports": {
".": {
"node": {
"require": "./index.cjs",
"import": "./index.js"
}
}
}
}
8 changes: 8 additions & 0 deletions e2e/esm-load-order/package.json
@@ -0,0 +1,8 @@
{
"type": "module",
"jest": {
"transform": {},
"testEnvironment": "node",
"preserveLoadOrder": true
}
}
1 change: 1 addition & 0 deletions packages/jest-config/src/Defaults.ts
Expand Up @@ -64,6 +64,7 @@ const defaultOptions: Config.DefaultOptions = {
notifyMode: 'failure-change',
openHandlesTimeout: 1000,
passWithNoTests: false,
preserveLoadOrder: false,
prettierPath: 'prettier',
resetMocks: false,
resetModules: false,
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/ValidConfig.ts
Expand Up @@ -120,6 +120,7 @@ export const initialOptions: Config.InitialOptions = {
onlyFailures: false,
openHandlesTimeout: 1000,
passWithNoTests: false,
preserveLoadOrder: false,
preset: 'react-native',
prettierPath: '<rootDir>/node_modules/prettier',
projects: ['project-a', 'project-b/'],
Expand Down Expand Up @@ -279,6 +280,7 @@ export const initialProjectOptions: Config.InitialProjectOptions = {
modulePathIgnorePatterns: ['<rootDir>/build/'],
modulePaths: ['/shared/vendor/modules'],
openHandlesTimeout: 1000,
preserveLoadOrder: false,
preset: 'react-native',
prettierPath: '<rootDir>/node_modules/prettier',
reporters: [
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/index.ts
Expand Up @@ -116,6 +116,7 @@ const groupOptions = (
openHandlesTimeout: options.openHandlesTimeout,
outputFile: options.outputFile,
passWithNoTests: options.passWithNoTests,
preserveLoadOrder: options.preserveLoadOrder,
projects: options.projects,
randomize: options.randomize,
replname: options.replname,
Expand Down Expand Up @@ -178,6 +179,7 @@ const groupOptions = (
modulePathIgnorePatterns: options.modulePathIgnorePatterns,
modulePaths: options.modulePaths,
openHandlesTimeout: options.openHandlesTimeout,
preserveLoadOrder: options.preserveLoadOrder,
prettierPath: options.prettierPath,
reporters: options.reporters,
resetMocks: options.resetMocks,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.ts
Expand Up @@ -922,6 +922,7 @@ export default async function normalize(
case 'openHandlesTimeout':
case 'outputFile':
case 'passWithNoTests':
case 'preserveLoadOrder':
case 'randomize':
case 'replname':
case 'resetMocks':
Expand Down
31 changes: 20 additions & 11 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -758,23 +758,32 @@ export default class Runtime {
}

if (module.status === 'unlinked') {
const preserveLoadOrder =
this._config.preserveLoadOrder || this._globalConfig?.preserveLoadOrder;
// ensure that every import fully evaluates before any siblings are allowed to import.
let linkPromiseChain = Promise.resolve();
let linkPromiseChain = (Promise<VMModule | void>).resolve();
// since we might attempt to link the same module in parallel, stick the promise in a weak map so every call to
// this method can await it
this._esmModuleLinkingMap.set(
module,
module.link((specifier: string, referencingModule: VMModule) => {
linkPromiseChain = linkPromiseChain.then(async () => {
const mod = await this.resolveModule(
specifier,
referencingModule.identifier,
referencingModule.context
);
await this.linkAndEvaluateModule(mod);
return mod;
});
return linkPromiseChain;
if (preserveLoadOrder) {
linkPromiseChain = linkPromiseChain.then(async () => {
const mod = await this.resolveModule<VMModule>(
specifier,
referencingModule.identifier,
referencingModule.context,
);
await this.linkAndEvaluateModule(mod);
return mod;
});
return linkPromiseChain;
}
return this.resolveModule<VMModule>(
specifier,
referencingModule.identifier,
referencingModule.context,
);
}),
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/jest-schemas/src/raw-types.ts
Expand Up @@ -288,6 +288,7 @@ export const RawInitialOptions = Type.Partial(
outputFile: Type.String(),
passWithNoTests: Type.Boolean(),
preset: Type.Union([Type.String(), Type.Null()]),
preserveLoadOrder: Type.Boolean(),
prettierPath: Type.Union([Type.String(), Type.Null()]),
projects: Type.Array(
Type.Union([
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-types/src/Config.ts
Expand Up @@ -182,6 +182,7 @@ export type DefaultOptions = {
notifyMode: NotifyMode;
openHandlesTimeout: number;
passWithNoTests: boolean;
preserveLoadOrder: boolean;
prettierPath: string;
resetMocks: boolean;
resetModules: boolean;
Expand Down Expand Up @@ -290,6 +291,7 @@ export type GlobalConfig = {
openHandlesTimeout: number;
passWithNoTests: boolean;
projects: Array<string>;
preserveLoadOrder: boolean;
randomize?: boolean;
replname?: string;
reporters?: Array<ReporterConfig>;
Expand Down Expand Up @@ -357,6 +359,7 @@ export type ProjectConfig = {
modulePaths?: Array<string>;
openHandlesTimeout: number;
preset?: string;
preserveLoadOrder: boolean;
prettierPath: string;
reporters: Array<string | ReporterConfig>;
resetMocks: boolean;
Expand Down Expand Up @@ -446,6 +449,7 @@ export type Argv = Arguments<
onlyFailures: boolean;
outputFile: string;
preset: string | null | undefined;
preserveLoadOrder: boolean;
prettierPath: string | null | undefined;
projects: Array<string>;
randomize: boolean;
Expand Down
2 changes: 2 additions & 0 deletions packages/test-utils/src/config.ts
Expand Up @@ -43,6 +43,7 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = {
openHandlesTimeout: 1000,
outputFile: undefined,
passWithNoTests: false,
preserveLoadOrder: false,
projects: [],
replname: undefined,
reporters: [],
Expand Down Expand Up @@ -99,6 +100,7 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = {
modulePathIgnorePatterns: [],
modulePaths: [],
openHandlesTimeout: 1000,
preserveLoadOrder: false,
prettierPath: 'prettier',
reporters: [
'default',
Expand Down

0 comments on commit f079261

Please sign in to comment.