|
| 1 | +import { jest } from "@jest/globals"; |
| 2 | +import { removeAttachedHandlers } from "../../src/cleanup/attached-handlers.mjs"; |
| 3 | + |
| 4 | +test("skips selected handlers and records removed events", () => { |
| 5 | + const handler = jest.fn(); |
| 6 | + const processObj = { off: jest.fn() }; |
| 7 | + const removedEvents = new Set(); |
| 8 | + const result = removeAttachedHandlers({ |
| 9 | + processObj, |
| 10 | + remove: "off", |
| 11 | + events: ["warning", "uncaughtException"], |
| 12 | + handlers: { warning: handler, uncaughtException: jest.fn() }, |
| 13 | + shouldSkip: (event) => event === "uncaughtException", |
| 14 | + markRemoved: (event) => removedEvents.add(event), |
| 15 | + }); |
| 16 | + |
| 17 | + expect(result).toEqual({ removalError: undefined, hasRemovalError: false }); |
| 18 | + expect(processObj.off).toHaveBeenCalledTimes(1); |
| 19 | + expect(processObj.off).toHaveBeenCalledWith("warning", handler); |
| 20 | + expect(removedEvents).toEqual(new Set(["warning", "uncaughtException"])); |
| 21 | +}); |
| 22 | + |
| 23 | +test("attempts all events and returns the first failure", () => { |
| 24 | + const first = Error("first removal"); |
| 25 | + const processObj = { |
| 26 | + off: jest |
| 27 | + .fn() |
| 28 | + .mockImplementationOnce(() => { |
| 29 | + throw first; |
| 30 | + }) |
| 31 | + .mockImplementationOnce(() => { |
| 32 | + throw Error("second removal"); |
| 33 | + }), |
| 34 | + }; |
| 35 | + const result = removeAttachedHandlers({ |
| 36 | + processObj, |
| 37 | + remove: "off", |
| 38 | + events: ["warning", "uncaughtException"], |
| 39 | + handlers: { warning: jest.fn(), uncaughtException: jest.fn() }, |
| 40 | + shouldSkip: () => false, |
| 41 | + markRemoved: jest.fn(), |
| 42 | + }); |
| 43 | + |
| 44 | + expect(result).toEqual({ removalError: first, hasRemovalError: true }); |
| 45 | + expect(processObj.off).toHaveBeenCalledTimes(2); |
| 46 | +}); |
0 commit comments