Skip to content

Commit 550c92f

Browse files
authored
chore: stabilize event emitter runners (#12641)
1 parent 0134332 commit 550c92f

File tree

3 files changed

+26
-87
lines changed

3 files changed

+26
-87
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
- `[jest-config]` [**BREAKING**] Rename `moduleLoader` to `runtime` ([#10817](https://github.com/facebook/jest/pull/10817))
1515
- `[jest-config]` [**BREAKING**] Rename `extraGlobals` to `sandboxInjectedGlobals` ([#10817](https://github.com/facebook/jest/pull/10817))
1616
- `[jest-config]` [**BREAKING**] Throw an error instead of showing a warning if multiple configs are used ([#12510](https://github.com/facebook/jest/pull/12510))
17-
- `[jest-core]` Pass project config to `globalSetup`/`globalTeardown` function as second argument ([#12440](https://github.com/facebook/jest/pull/12440))
1817
- `[jest-cli, jest-core]` Add `--ignoreProjects` CLI argument to ignore test suites by project name ([#12620](https://github.com/facebook/jest/pull/12620))
18+
- `[jest-core]` Pass project config to `globalSetup`/`globalTeardown` function as second argument ([#12440](https://github.com/facebook/jest/pull/12440))
19+
- `[jest-core]` Stabilize test runners with event emitters ([#12641](https://github.com/facebook/jest/pull/12641))
1920
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade jsdom to 19.0.0 ([#12290](https://github.com/facebook/jest/pull/12290))
2021
- `[jest-environment-jsdom]` [**BREAKING**] Add default `browser` condition to `exportConditions` for `jsdom` environment ([#11924](https://github.com/facebook/jest/pull/11924))
2122
- `[jest-environment-jsdom]` [**BREAKING**] Pass global config to Jest environment constructor for `jsdom` environment ([#12461](https://github.com/facebook/jest/pull/12461))

packages/jest-core/src/TestScheduler.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,7 @@ class TestScheduler {
242242
serial: runInBand || Boolean(testRunner.isSerial),
243243
};
244244

245-
/**
246-
* Test runners with event emitters are still not supported
247-
* for third party test runners.
248-
*/
249-
if (testRunner.__PRIVATE_UNSTABLE_API_supportsEventEmitters__) {
245+
if (testRunner.supportsEventEmitters) {
250246
const unsubscribes = [
251247
testRunner.on('test-file-start', ([test]) =>
252248
onTestFileStart(test),

packages/jest-runner/src/index.ts

+23-81
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default class TestRunner {
4747
private readonly _globalConfig: Config.GlobalConfig;
4848
private readonly _context: TestRunnerContext;
4949
private readonly eventEmitter = new Emittery<TestEvents>();
50-
readonly __PRIVATE_UNSTABLE_API_supportsEventEmitters__: boolean = true;
50+
readonly supportsEventEmitters: boolean = true;
5151

5252
readonly isSerial?: boolean;
5353

@@ -59,29 +59,19 @@ export default class TestRunner {
5959
async runTests(
6060
tests: Array<Test>,
6161
watcher: TestWatcher,
62-
onStart: OnTestStart | undefined,
63-
onResult: OnTestSuccess | undefined,
64-
onFailure: OnTestFailure | undefined,
62+
// keep these three as they're still passed and should be in the types,
63+
// even if this particular runner doesn't use them
64+
_onStart: OnTestStart | undefined,
65+
_onResult: OnTestSuccess | undefined,
66+
_onFailure: OnTestFailure | undefined,
6567
options: TestRunnerOptions,
6668
): Promise<void> {
6769
return await (options.serial
68-
? this._createInBandTestRun(tests, watcher, onStart, onResult, onFailure)
69-
: this._createParallelTestRun(
70-
tests,
71-
watcher,
72-
onStart,
73-
onResult,
74-
onFailure,
75-
));
70+
? this._createInBandTestRun(tests, watcher)
71+
: this._createParallelTestRun(tests, watcher));
7672
}
7773

78-
private async _createInBandTestRun(
79-
tests: Array<Test>,
80-
watcher: TestWatcher,
81-
onStart?: OnTestStart,
82-
onResult?: OnTestSuccess,
83-
onFailure?: OnTestFailure,
84-
) {
74+
private async _createInBandTestRun(tests: Array<Test>, watcher: TestWatcher) {
8575
process.env.JEST_WORKER_ID = '1';
8676
const mutex = throat(1);
8777
return tests.reduce(
@@ -92,19 +82,6 @@ export default class TestRunner {
9282
if (watcher.isInterrupted()) {
9383
throw new CancelRun();
9484
}
95-
// Remove `if(onStart)` in Jest 27
96-
if (onStart) {
97-
await onStart(test);
98-
99-
return runTest(
100-
test.path,
101-
this._globalConfig,
102-
test.context.config,
103-
test.context.resolver,
104-
this._context,
105-
undefined,
106-
);
107-
}
10885

10986
// `deepCyclicCopy` used here to avoid mem-leak
11087
const sendMessageToJest: TestFileEvent = (eventName, args) =>
@@ -124,23 +101,11 @@ export default class TestRunner {
124101
sendMessageToJest,
125102
);
126103
})
127-
.then(result => {
128-
if (onResult) {
129-
return onResult(test, result);
130-
}
131-
132-
return this.eventEmitter.emit('test-file-success', [
133-
test,
134-
result,
135-
]);
136-
})
137-
.catch(err => {
138-
if (onFailure) {
139-
return onFailure(test, err);
140-
}
141-
142-
return this.eventEmitter.emit('test-file-failure', [test, err]);
143-
}),
104+
.then(
105+
result =>
106+
this.eventEmitter.emit('test-file-success', [test, result]),
107+
err => this.eventEmitter.emit('test-file-failure', [test, err]),
108+
),
144109
),
145110
Promise.resolve(),
146111
);
@@ -149,9 +114,6 @@ export default class TestRunner {
149114
private async _createParallelTestRun(
150115
tests: Array<Test>,
151116
watcher: TestWatcher,
152-
onStart?: OnTestStart,
153-
onResult?: OnTestSuccess,
154-
onFailure?: OnTestFailure,
155117
) {
156118
const resolvers: Map<string, SerializableResolver> = new Map();
157119
for (const test of tests) {
@@ -168,11 +130,7 @@ export default class TestRunner {
168130
forkOptions: {stdio: 'pipe'},
169131
maxRetries: 3,
170132
numWorkers: this._globalConfig.maxWorkers,
171-
setupArgs: [
172-
{
173-
serializableResolvers: Array.from(resolvers.values()),
174-
},
175-
],
133+
setupArgs: [{serializableResolvers: Array.from(resolvers.values())}],
176134
}) as WorkerInterface;
177135

178136
if (worker.getStdout()) worker.getStdout().pipe(process.stdout);
@@ -188,12 +146,7 @@ export default class TestRunner {
188146
return Promise.reject();
189147
}
190148

191-
// Remove `if(onStart)` in Jest 27
192-
if (onStart) {
193-
await onStart(test);
194-
} else {
195-
await this.eventEmitter.emit('test-file-start', [test]);
196-
}
149+
await this.eventEmitter.emit('test-file-start', [test]);
197150

198151
const promise = worker.worker({
199152
config: test.context.config,
@@ -212,9 +165,9 @@ export default class TestRunner {
212165

213166
if (promise.UNSTABLE_onCustomMessage) {
214167
// TODO: Get appropriate type for `onCustomMessage`
215-
promise.UNSTABLE_onCustomMessage(([event, payload]: any) => {
216-
this.eventEmitter.emit(event, payload);
217-
});
168+
promise.UNSTABLE_onCustomMessage(([event, payload]: any) =>
169+
this.eventEmitter.emit(event, payload),
170+
);
218171
}
219172

220173
return promise;
@@ -230,21 +183,10 @@ export default class TestRunner {
230183

231184
const runAllTests = Promise.all(
232185
tests.map(test =>
233-
runTestInWorker(test)
234-
.then(result => {
235-
if (onResult) {
236-
return onResult(test, result);
237-
}
238-
239-
return this.eventEmitter.emit('test-file-success', [test, result]);
240-
})
241-
.catch(error => {
242-
if (onFailure) {
243-
return onFailure(test, error);
244-
}
245-
246-
return this.eventEmitter.emit('test-file-failure', [test, error]);
247-
}),
186+
runTestInWorker(test).then(
187+
result => this.eventEmitter.emit('test-file-success', [test, result]),
188+
error => this.eventEmitter.emit('test-file-failure', [test, error]),
189+
),
248190
),
249191
);
250192

0 commit comments

Comments
 (0)