From ea5cb3ad020d9d925ecd0ce233001e0747b6498c Mon Sep 17 00:00:00 2001 From: unraid Date: Sat, 9 May 2026 16:12:28 +0800 Subject: [PATCH] test: gate lanBeacon dgram mock behind a per-suite flag Same spread+flag pattern as the axios / child_process polluters: the bare `mock.module('dgram', () => ({ createSocket: () => mockSocket }))` leaked the stub into every later test file in the run via Bun's last-write-wins module mock cache. Now we spread real dgram and gate `createSocket` through `useLanBeaconDgramStubs`, flipped on in beforeAll and off in afterAll so unrelated UDP-using code in later suites sees real dgram. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/utils/__tests__/lanBeacon.test.ts | 40 ++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/utils/__tests__/lanBeacon.test.ts b/src/utils/__tests__/lanBeacon.test.ts index 561f89cca..f63ab7508 100644 --- a/src/utils/__tests__/lanBeacon.test.ts +++ b/src/utils/__tests__/lanBeacon.test.ts @@ -1,4 +1,13 @@ -import { describe, test, expect, mock, beforeEach, afterEach } from 'bun:test' +import { + afterAll, + afterEach, + beforeAll, + beforeEach, + describe, + expect, + mock, + test, +} from 'bun:test' // Mock dgram before importing LanBeacon const mockSocket = { @@ -13,9 +22,32 @@ const mockSocket = { close: mock(() => {}), } -mock.module('dgram', () => ({ - createSocket: () => mockSocket, -})) +// Spread+flag pattern: previously this was a bare `mock.module('dgram', ...)` +// which leaked the stub createSocket into every later test file in the +// process via Bun's last-write-wins module mock cache. Spread real dgram +// + gate the stub behind useLanBeaconDgramStubs so other tests see real UDP. +let useLanBeaconDgramStubs = false +mock.module('dgram', () => { + // eslint-disable-next-line @typescript-eslint/no-require-imports + const real = require('dgram') as Record + return { + ...real, + default: real, + createSocket: ((...args: unknown[]) => + useLanBeaconDgramStubs + ? mockSocket + : (real.createSocket as (...a: unknown[]) => unknown)( + ...args, + )) as typeof real.createSocket, + } +}) + +beforeAll(() => { + useLanBeaconDgramStubs = true +}) +afterAll(() => { + useLanBeaconDgramStubs = false +}) const { LanBeacon } = await import('../lanBeacon.js')