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