|
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 | ];
|
@@ -49,17 +49,24 @@ export const setState = (state: Circus.State): Circus.State =>
|
49 | 49 | /* eslint-enable */
|
50 | 50 |
|
51 | 51 | export const dispatch = async (event: Circus.AsyncEvent): Promise<void> => {
|
52 |
| - for (const handler of eventHandlers) { |
| 52 | + for (const handler of global[EVENT_HANDLERS]) { |
53 | 53 | await handler(event, getState());
|
54 | 54 | }
|
55 | 55 | };
|
56 | 56 |
|
57 | 57 | export const dispatchSync = (event: Circus.SyncEvent): void => {
|
58 |
| - for (const handler of eventHandlers) { |
| 58 | + for (const handler of global[EVENT_HANDLERS]) { |
59 | 59 | handler(event, getState());
|
60 | 60 | }
|
61 | 61 | };
|
62 | 62 |
|
63 | 63 | export const addEventHandler = (handler: Circus.EventHandler): void => {
|
64 |
| - eventHandlers.push(handler); |
| 64 | + global[EVENT_HANDLERS].push(handler); |
| 65 | +}; |
| 66 | + |
| 67 | +export const removeEventHandler = (handler: Circus.EventHandler): void => { |
| 68 | + const index = global[EVENT_HANDLERS].lastIndexOf(handler); |
| 69 | + if (index !== -1) { |
| 70 | + global[EVENT_HANDLERS].splice(index, 1); |
| 71 | + } |
65 | 72 | };
|
0 commit comments