mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-22 08:15:53 +00:00
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) <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
// Mock dgram before importing LanBeacon
|
||||||
const mockSocket = {
|
const mockSocket = {
|
||||||
@@ -13,9 +22,32 @@ const mockSocket = {
|
|||||||
close: mock(() => {}),
|
close: mock(() => {}),
|
||||||
}
|
}
|
||||||
|
|
||||||
mock.module('dgram', () => ({
|
// Spread+flag pattern: previously this was a bare `mock.module('dgram', ...)`
|
||||||
createSocket: () => mockSocket,
|
// 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<string, unknown>
|
||||||
|
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')
|
const { LanBeacon } = await import('../lanBeacon.js')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user