|
8 | 8 | import type {Circus} from '@jest/types';
|
9 | 9 | import eventHandler from './eventHandler';
|
10 | 10 | import formatNodeAssertErrors from './formatNodeAssertErrors';
|
11 |
| -import {STATE_SYM} from './types'; |
| 11 | +import {EVENT_HANDLERS, STATE_SYM} from './types'; |
12 | 12 | import {makeDescribe} from './utils';
|
13 | 13 |
|
14 |
| -const eventHandlers: Array<Circus.EventHandler> = [ |
| 14 | +global[EVENT_HANDLERS] = global[EVENT_HANDLERS] || [ |
15 | 15 | eventHandler,
|
16 | 16 | formatNodeAssertErrors,
|
17 | 17 | ];
|
@@ -46,17 +46,24 @@ export const setState = (state: Circus.State): Circus.State =>
|
46 | 46 | (global[STATE_SYM] = state);
|
47 | 47 |
|
48 | 48 | export const dispatch = async (event: Circus.AsyncEvent): Promise<void> => {
|
49 |
| - for (const handler of eventHandlers) { |
| 49 | + for (const handler of global[EVENT_HANDLERS]) { |
50 | 50 | await handler(event, getState());
|
51 | 51 | }
|
52 | 52 | };
|
53 | 53 |
|
54 | 54 | export const dispatchSync = (event: Circus.SyncEvent): void => {
|
55 |
| - for (const handler of eventHandlers) { |
| 55 | + for (const handler of global[EVENT_HANDLERS]) { |
56 | 56 | handler(event, getState());
|
57 | 57 | }
|
58 | 58 | };
|
59 | 59 |
|
60 | 60 | 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 | + } |
62 | 69 | };
|
0 commit comments