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