Skip to content

Commit

Permalink
feat: bundle Jest's modules with webpack (#12348)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Sep 19, 2023
1 parent e2326fa commit 70340d0
Show file tree
Hide file tree
Showing 20 changed files with 330 additions and 173 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

### Performance

- `[*]` [**BREAKING**] Bundle all of Jest's modules into `index.js` ([#12348](https://github.com/jestjs/jest/pull/12348))

### Chore & Maintenance

- `[*]` [**BREAKING**] Drop support for Node.js versions 14 and 19 ([#14460](https://github.com/jestjs/jest/pull/14460))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = `
14 | });
15 | });
at eventHandler (../../packages/jest-circus/build/eventHandler.js:141:11)
at eventHandler (../../packages/jest-circus/build/jestAdapterInit.js:148:38)
at test (__tests__/asyncDefinition.test.js:12:5)
● Test suite failed to run
Expand All @@ -31,7 +31,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = `
15 | });
16 |
at eventHandler (../../packages/jest-circus/build/eventHandler.js:104:11)
at eventHandler (../../packages/jest-circus/build/jestAdapterInit.js:114:38)
at afterAll (__tests__/asyncDefinition.test.js:13:5)
● Test suite failed to run
Expand All @@ -46,7 +46,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = `
20 | });
21 |
at eventHandler (../../packages/jest-circus/build/eventHandler.js:141:11)
at eventHandler (../../packages/jest-circus/build/jestAdapterInit.js:148:38)
at test (__tests__/asyncDefinition.test.js:18:3)
● Test suite failed to run
Expand All @@ -60,6 +60,6 @@ exports[`defining tests and hooks asynchronously throws 1`] = `
20 | });
21 |
at eventHandler (../../packages/jest-circus/build/eventHandler.js:104:11)
at eventHandler (../../packages/jest-circus/build/jestAdapterInit.js:114:38)
at afterAll (__tests__/asyncDefinition.test.js:19:3)"
`;
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ exports[`moduleNameMapper wrong array configuration 1`] = `
12 | module.exports = () => 'test';
13 |
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:759:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1175:17)
at Object.require (index.js:10:1)
at Object.require (__tests__/index.js:10:20)"
`;
Expand Down Expand Up @@ -71,7 +71,7 @@ exports[`moduleNameMapper wrong configuration 1`] = `
12 | module.exports = () => 'test';
13 |
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:759:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1175:17)
at Object.require (index.js:10:1)
at Object.require (__tests__/index.js:10:20)"
`;
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ exports[`shows a proper error from deep requires 1`] = `
12 | test('dummy', () => {
13 | expect(1).toBe(1);
at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:427:11)
at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/index.js:930:11)
at Object.<anonymous> (node_modules/discord.js/src/index.js:21:12)
at Object.require (__tests__/test.js:10:1)"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exports[`show error message with matching files 1`] = `
| ^
9 |
at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:427:11)
at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/index.js:930:11)
at Object.require (index.js:8:18)
at Object.require (__tests__/test.js:8:11)"
`;
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
"/node_modules/"
],
"testRegex": [],
"testRunner": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-circus/runner.js",
"testRunner": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-circus/build/runner.js",
"transform": [
[
"\\\\.[jt]sx?$",
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/testFailingJasmine.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ FAIL __tests__/worksWithConcurrentMode.test.js
15 | });
16 |
at Function.failing (../../packages/jest-jasmine2/build/jasmineAsyncInstall.js:175:11)
at Function.failing (../../packages/jest-jasmine2/build/index.js:308:17)
at Suite.failing (__tests__/worksWithConcurrentMode.test.js:13:17)
at Object.describe (__tests__/worksWithConcurrentMode.test.js:8:1)
Expand Down Expand Up @@ -80,7 +80,7 @@ FAIL __tests__/worksWithConcurrentOnlyMode.test.js
15 | });
16 |
at Function.failing (../../packages/jest-jasmine2/build/jasmineAsyncInstall.js:175:11)
at Function.failing (../../packages/jest-jasmine2/build/index.js:308:17)
at Suite.failing (__tests__/worksWithConcurrentOnlyMode.test.js:13:22)
at Object.describe (__tests__/worksWithConcurrentOnlyMode.test.js:8:1)
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`babel-jest ignored tells user to match ignored files 1`] = `
babel-jest: Babel ignores __tests__/ignoredFile.test.js - make sure to include the file in Jest's transformIgnorePatterns as well.
at assertLoadedBabelConfig (../../../packages/babel-jest/build/index.js:105:11)"
at assertLoadedBabelConfig (../../../packages/babel-jest/build/index.js:138:11)"
`;

exports[`babel-jest instruments only specific files and collects coverage 1`] = `
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/stackTrace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('Stack Trace', () => {
);

expect(stderr).toMatch(
/\s+at\s(?:.+?)\s\((?:.+?)jest-resolve\/build\/resolver\.js/,
/\s+at\s(?:.+?)\s\((?:.+?)jest-resolve\/build\/index\.js/,
);
});

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"ansi-regex": "^5.0.1",
"ansi-styles": "^5.0.0",
"babel-jest": "workspace:^",
"babel-loader": "^8.2.3",
"camelcase": "^6.2.0",
"chalk": "^4.0.0",
"chokidar": "^3.3.0",
Expand Down Expand Up @@ -80,6 +81,8 @@
"tempy": "^1.0.0",
"ts-node": "^10.5.0",
"typescript": "^5.0.4",
"webpack": "^5.68.0",
"webpack-node-externals": "^3.0.0",
"which": "^4.0.0"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-circus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"default": "./build/index.js"
},
"./package.json": "./package.json",
"./runner": "./runner.js"
"./runner": "./build/runner.js"
},
"dependencies": {
"@jest/environment": "workspace:^",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
*/

// Allow people to use `jest-circus/runner` as a runner.
const runner = require('./build/legacy-code-todo-rewrite/jestAdapter').default;
module.exports = runner;
import runner from './legacy-code-todo-rewrite/jestAdapter';

export default runner;
13 changes: 8 additions & 5 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export type AllOptions = Config.ProjectConfig & Config.GlobalConfig;
const createConfigError = (message: string) =>
new ValidationError(ERROR, message, DOCUMENTATION_NOTE);

// we wanna avoid webpack trying to be clever
const requireResolve = (module: string) => require.resolve(module);

function verifyDirectoryExists(path: string, key: string) {
try {
const rootStat = statSync(path);
Expand Down Expand Up @@ -525,7 +528,7 @@ export default async function normalize(
}

options.testEnvironment = resolveTestEnvironment({
requireResolveFunction: require.resolve,
requireResolveFunction: requireResolve,
rootDir: options.rootDir,
testEnvironment:
options.testEnvironment ||
Expand Down Expand Up @@ -667,7 +670,7 @@ export default async function normalize(
option &&
resolveRunner(newOptions.resolver, {
filePath: option,
requireResolveFunction: require.resolve,
requireResolveFunction: requireResolve,
rootDir: options.rootDir,
});
}
Expand Down Expand Up @@ -951,7 +954,7 @@ export default async function normalize(
config: {},
path: resolveWatchPlugin(newOptions.resolver, {
filePath: watchPlugin,
requireResolveFunction: require.resolve,
requireResolveFunction: requireResolve,
rootDir: options.rootDir,
}),
};
Expand All @@ -960,7 +963,7 @@ export default async function normalize(
config: watchPlugin[1] || {},
path: resolveWatchPlugin(newOptions.resolver, {
filePath: watchPlugin[0],
requireResolveFunction: require.resolve,
requireResolveFunction: requireResolve,
rootDir: options.rootDir,
}),
};
Expand Down Expand Up @@ -995,7 +998,7 @@ export default async function normalize(
newOptions.testSequencer = resolveSequencer(newOptions.resolver, {
filePath:
options.testSequencer || require.resolve(DEFAULT_CONFIG.testSequencer),
requireResolveFunction: require.resolve,
requireResolveFunction: requireResolve,
rootDir: options.rootDir,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/readConfigFileAndSetRootDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function getRegisteredCompiler() {
async function registerTsNode(): Promise<Service> {
try {
// Register TypeScript compiler instance
const tsNode = await import('ts-node');
const tsNode = await import(/* webpackIgnore: true */ 'ts-node');
return tsNode.register({
compilerOptions: {
module: 'CommonJS',
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ async function runTestInternal(
}
testEnvironment = resolveTestEnvironment({
...projectConfig,
requireResolveFunction: require.resolve,
// we wanna avoid webpack trying to be clever
requireResolveFunction: module => require.resolve(module),
testEnvironment: customEnvironment,
});
}
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-util/src/requireOrImportModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export default async function requireOrImportModule<T>(
const moduleUrl = pathToFileURL(filePath);

// node `import()` supports URL, but TypeScript doesn't know that
const importedModule = await import(moduleUrl.href);
const importedModule = await import(
/* webpackIgnore: true */ moduleUrl.href
);

if (!applyInteropRequireDefault) {
return importedModule;
Expand Down
Loading

0 comments on commit 70340d0

Please sign in to comment.