Skip to content

Commit 7ec8c2a

Browse files
committed
fix: jest-circus shares events among imports jestjs#11483
1 parent d7f0975 commit 7ec8c2a

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Fixes
66

7+
- `[jest-circus]` Shares events with event handlers among imports ([#11483](https://github.com/facebook/jest/pull/11483))
8+
79
### Chore & Maintenance
810

911
### Performance

packages/jest-circus/src/__mocks__/testUtils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export const runTest = (source: string) => {
4646
4747
const testEventHandler = require('${TEST_EVENT_HANDLER_PATH}').default;
4848
const addEventHandler = require('${CIRCUS_STATE_PATH}').addEventHandler;
49+
const removeEventHandler = require('${CIRCUS_STATE_PATH}').removeEventHandler;
4950
addEventHandler(testEventHandler);
5051
5152
${source};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// addEventHandler and removeEventHandler are provided in the ./index
9+
import {addEventHandler, removeEventHandler} from '../index';
10+
// dispatch comes from the ./state
11+
import {dispatch} from '../state';
12+
13+
test('addEventHandler and removeEventHandler control handlers', async () => {
14+
const spy = jest.fn();
15+
16+
addEventHandler(spy);
17+
expect(spy).not.toHaveBeenCalledWith({name: 'unknown1'}, expect.anything());
18+
await dispatch({name: 'unknown1' as any});
19+
expect(spy).toHaveBeenCalledWith({name: 'unknown1'}, expect.anything());
20+
21+
removeEventHandler(spy);
22+
expect(spy).not.toHaveBeenCalledWith({name: 'unknown2'}, expect.anything());
23+
await dispatch({name: 'unknown2' as any});
24+
expect(spy).not.toHaveBeenCalledWith({name: 'unknown2'}, expect.anything());
25+
});

packages/jest-circus/src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import {bind as bindEach} from 'jest-each';
1010
import {ErrorWithStack, isPromise} from 'jest-util';
1111
import {dispatchSync} from './state';
1212

13-
export {setState, getState, resetState} from './state';
13+
export {
14+
setState,
15+
getState,
16+
resetState,
17+
addEventHandler,
18+
removeEventHandler,
19+
} from './state';
1420
export {default as run} from './run';
1521

1622
type THook = (fn: Circus.HookFn, timeout?: number) => void;

packages/jest-circus/src/state.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import type {Circus} from '@jest/types';
99
import eventHandler from './eventHandler';
1010
import formatNodeAssertErrors from './formatNodeAssertErrors';
11-
import {STATE_SYM} from './types';
11+
import {EVENT_HANDLERS, STATE_SYM} from './types';
1212
import {makeDescribe} from './utils';
1313

14-
const eventHandlers: Array<Circus.EventHandler> = [
14+
global[EVENT_HANDLERS] = global[EVENT_HANDLERS] || [
1515
eventHandler,
1616
formatNodeAssertErrors,
1717
];
@@ -46,17 +46,24 @@ export const setState = (state: Circus.State): Circus.State =>
4646
(global[STATE_SYM] = state);
4747

4848
export const dispatch = async (event: Circus.AsyncEvent): Promise<void> => {
49-
for (const handler of eventHandlers) {
49+
for (const handler of global[EVENT_HANDLERS]) {
5050
await handler(event, getState());
5151
}
5252
};
5353

5454
export const dispatchSync = (event: Circus.SyncEvent): void => {
55-
for (const handler of eventHandlers) {
55+
for (const handler of global[EVENT_HANDLERS]) {
5656
handler(event, getState());
5757
}
5858
};
5959

6060
export const addEventHandler = (handler: Circus.EventHandler): void => {
61-
eventHandlers.push(handler);
61+
global[EVENT_HANDLERS].push(handler);
62+
};
63+
64+
export const removeEventHandler = (handler: Circus.EventHandler): void => {
65+
const index = global[EVENT_HANDLERS].lastIndexOf(handler);
66+
if (index !== -1) {
67+
global[EVENT_HANDLERS].splice(index, 1);
68+
}
6269
};

packages/jest-circus/src/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ export const RETRY_TIMES = Symbol.for(
1919
export const TEST_TIMEOUT_SYMBOL = Symbol.for(
2020
'TEST_TIMEOUT_SYMBOL',
2121
) as unknown as 'TEST_TIMEOUT_SYMBOL';
22+
export const EVENT_HANDLERS = Symbol.for(
23+
'EVENT_HANDLERS',
24+
) as unknown as 'EVENT_HANDLERS';
2225

2326
declare global {
2427
module NodeJS {
2528
interface Global {
2629
STATE_SYM_SYMBOL: Circus.State;
2730
RETRY_TIMES_SYMBOL: string;
2831
TEST_TIMEOUT_SYMBOL: number;
32+
EVENT_HANDLERS: Array<Circus.EventHandler>;
2933
expect: typeof expect;
3034
}
3135
}

0 commit comments

Comments
 (0)