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