|
| 1 | +/** |
| 2 | + * Jest setup for jsdom-based browser UI tests. |
| 3 | + * |
| 4 | + * These tests mount the full app, which schedules a bunch of background React |
| 5 | + * updates (Radix tooltips/selects, async effects like branch loading, etc.). |
| 6 | + * In jsdom this can produce extremely noisy act(...) warnings that drown out |
| 7 | + * real failures. |
| 8 | + */ |
| 9 | + |
| 10 | +import { EventEmitter } from "events"; |
| 11 | + |
| 12 | +const originalConsoleError = console.error.bind(console); |
| 13 | +const originalDefaultMaxListeners = EventEmitter.defaultMaxListeners; |
| 14 | +const originalConsoleLog = console.log.bind(console); |
| 15 | + |
| 16 | +const shouldSuppressActWarning = (args: unknown[]) => { |
| 17 | + return args.some( |
| 18 | + (arg) => typeof arg === "string" && arg.toLowerCase().includes("not wrapped in act") |
| 19 | + ); |
| 20 | +}; |
| 21 | + |
| 22 | +beforeAll(() => { |
| 23 | + // The full app creates a bunch of subscriptions on a single EventEmitter |
| 24 | + // (ProviderService configChanged). That's OK for these tests, but Node warns |
| 25 | + // once the default (10) listener limit is exceeded. |
| 26 | + EventEmitter.defaultMaxListeners = 50; |
| 27 | + jest.spyOn(console, "error").mockImplementation((...args) => { |
| 28 | + if (shouldSuppressActWarning(args)) { |
| 29 | + return; |
| 30 | + } |
| 31 | + originalConsoleError(...args); |
| 32 | + }); |
| 33 | + |
| 34 | + // Keep the test output focused; individual tests can temporarily unmock if |
| 35 | + // they need to assert on logs. |
| 36 | + jest.spyOn(console, "log").mockImplementation(() => {}); |
| 37 | + jest.spyOn(console, "warn").mockImplementation(() => {}); |
| 38 | +}); |
| 39 | + |
| 40 | +afterAll(() => { |
| 41 | + EventEmitter.defaultMaxListeners = originalDefaultMaxListeners; |
| 42 | + (console.error as jest.Mock).mockRestore(); |
| 43 | + (console.log as jest.Mock).mockRestore(); |
| 44 | + (console.warn as jest.Mock).mockRestore(); |
| 45 | + |
| 46 | + // Ensure captured originals don't get tree-shaken / flagged as unused in some tooling. |
| 47 | + void originalConsoleLog; |
| 48 | +}); |
0 commit comments