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