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