Skip to content

Commit

Permalink
jestjs#8708 Per-worker global setup/teardown (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalroth committed Jul 28, 2024
1 parent b113b44 commit 592768c
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 105 deletions.
6 changes: 6 additions & 0 deletions packages/jest-cli/src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ export const options: {[key: string]: Options} = {
requiresArg: true,
type: 'string',
},
globalHooksPerWorker: {
// TODO description
description: 'foo',
requiresArg: true,
type: 'boolean',
},
globals: {
description:
'A JSON string with map of global variables that need ' +
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/Descriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const descriptions: {[key in keyof Config.InitialOptions]: string} = {
'A path to a module which exports an async function that is triggered once before all test suites',
globalTeardown:
'A path to a module which exports an async function that is triggered once after all test suites',
// TODO description
globalHooksPerWorker: 'foo',
globals:
'A set of global variables that need to be available in all test environments',
maxWorkers:
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/ValidConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const initialOptions: Config.InitialOptions = {
filter: '<rootDir>/filter.js',
forceCoverageMatch: ['**/*.t.js'],
forceExit: false,
globalHooksPerWorker: false,
globalSetup: 'setup.js',
globalTeardown: 'teardown.js',
globals: {__DEV__: true},
Expand Down Expand Up @@ -244,6 +245,7 @@ export const initialProjectOptions: Config.InitialProjectOptions = {
},
filter: '<rootDir>/filter.js',
forceCoverageMatch: ['**/*.t.js'],
globalHooksPerWorker: false,
globalSetup: 'setup.js',
globalTeardown: 'teardown.js',
globals: {__DEV__: true},
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const groupOptions = (
forceExit: options.forceExit,
globalSetup: options.globalSetup,
globalTeardown: options.globalTeardown,
globalHooksPerWorker: options.globalHooksPerWorker,
json: options.json,
lastCommit: options.lastCommit,
listTests: options.listTests,
Expand Down Expand Up @@ -168,6 +169,7 @@ const groupOptions = (
forceCoverageMatch: options.forceCoverageMatch,
globalSetup: options.globalSetup,
globalTeardown: options.globalTeardown,
globalHooksPerWorker: options.globalHooksPerWorker,
globals: options.globals,
haste: options.haste,
id: options.id,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ export default async function normalize(
case 'expand':
case 'extensionsToTreatAsEsm':
case 'globals':
case 'globalHooksPerWorker':
case 'fakeTimers':
case 'findRelatedTests':
case 'forceCoverageMatch':
Expand Down
84 changes: 0 additions & 84 deletions packages/jest-core/src/runGlobalHook.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import collectNodeHandles, {
type HandleCollectionResult,
} from './collectHandles';
import getNoTestsFoundMessage from './getNoTestsFoundMessage';
import runGlobalHook from './runGlobalHook';
import type {Filter, TestRunData} from './types';

const getTestPaths = async (
Expand Down Expand Up @@ -279,12 +278,6 @@ export default async function runJest({
collectHandles = collectNodeHandles();
}

if (hasTests) {
performance.mark('jest/globalSetup:start');
await runGlobalHook({allTests, globalConfig, moduleName: 'globalSetup'});
performance.mark('jest/globalSetup:end');
}

if (changedFilesPromise) {
const changedFilesInfo = await changedFilesPromise;
if (changedFilesInfo.changedFiles) {
Expand Down Expand Up @@ -320,12 +313,6 @@ export default async function runJest({
sequencer.cacheResults(allTests, results);
performance.mark('jest/cacheResults:end');

if (hasTests) {
performance.mark('jest/globalTeardown:start');
await runGlobalHook({allTests, globalConfig, moduleName: 'globalTeardown'});
performance.mark('jest/globalTeardown:end');
}

performance.mark('jest/processResults:start');
await processResults(results, {
collectHandles,
Expand Down
54 changes: 52 additions & 2 deletions packages/jest-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
type PromiseWithCustomMessage,
Worker,
} from 'jest-worker';
import runGlobalHooks from './runGlobalHooks';
import runTest from './runTest';
import type {SerializableResolver} from './testWorker';
import {
Expand Down Expand Up @@ -62,8 +63,16 @@ export default class TestRunner extends EmittingTestRunner {

async #createInBandTestRun(tests: Array<Test>, watcher: TestWatcher) {
process.env.JEST_WORKER_ID = '1';

await runGlobalHooks({
projectConfig: tests[0].context.config,
globalConfig: this._globalConfig,
moduleName: 'globalSetup',
runInBand: true,
});

const mutex = pLimit(1);
return tests.reduce(
const result = await tests.reduce(
(promise, test) =>
mutex(() =>
promise
Expand Down Expand Up @@ -92,6 +101,15 @@ export default class TestRunner extends EmittingTestRunner {
),
Promise.resolve(),
);

await runGlobalHooks({
projectConfig: tests[0].context.config,
globalConfig: this._globalConfig,
moduleName: 'globalTeardown',
runInBand: true,
});

return result;
}

async #createParallelTestRun(tests: Array<Test>, watcher: TestWatcher) {
Expand Down Expand Up @@ -170,6 +188,20 @@ export default class TestRunner extends EmittingTestRunner {
});
});

await runGlobalHooks({
projectConfig: tests[0].context.config,
globalConfig: this._globalConfig,
moduleName: 'globalSetup',
runInBand: false,
});

await worker.runInAllWorkers('runGlobal', {
projectConfig: tests[0].context.config,
globalConfig: this._globalConfig,
moduleName: 'globalSetup',
runInBand: false,
});

const runAllTests = Promise.all(
tests.map(test =>
runTestInWorker(test).then(
Expand All @@ -181,6 +213,12 @@ export default class TestRunner extends EmittingTestRunner {
);

const cleanup = async () => {
await worker.runInAllWorkers('runGlobal', {
projectConfig: tests[0].context.config,
globalConfig: this._globalConfig,
moduleName: 'globalTeardown',
runInBand: false,
});
const {forceExited} = await worker.end();
if (forceExited) {
console.error(
Expand All @@ -194,7 +232,19 @@ export default class TestRunner extends EmittingTestRunner {
}
};

return Promise.race([runAllTests, onInterrupt]).then(cleanup, cleanup);
const result = await Promise.race([runAllTests, onInterrupt]).then(
cleanup,
cleanup,
);

await runGlobalHooks({
projectConfig: tests[0].context.config,
globalConfig: this._globalConfig,
moduleName: 'globalTeardown',
runInBand: false,
});

return result;
}

on<Name extends keyof TestEvents>(
Expand Down
Loading

0 comments on commit 592768c

Please sign in to comment.