Skip to content

Commit cae8d3a

Browse files
committed
fix(tests/cover): mock spawn via src/ relative path so threaded-pool dedup catches the prod instance
Both cover/type.test.mts and cover/code.test.mts used: vi.mock('@socketsecurity/lib/process/spawn/child') That mock keys on the package-specifier resolution. Inside the production source (src/cover/type.ts, src/cover/code.ts), the import is relative: import { spawn } from '../process/spawn/child' Under vitest's threaded pool, the two resolution paths land on different module instances despite the alias collapsing them to the same file. The mock applies to the package-path instance; the production code uses the relative-path instance — uncoupled. Fix matches the canonical pattern from test/unit/releases/*.test.mts: mock via the src/ relative path so both sides resolve to the same key in vitest's mock registry. cover/type.test.mts: 4 previously-failing tests now pass (10/10). cover/code.test.mts: 1 previously-failing test now passes (29/29).
1 parent 4f933e2 commit cae8d3a

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

test/unit/cover/code.test.mts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ import path from 'node:path'
1212

1313
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
1414

15-
vi.mock('@socketsecurity/lib/process/spawn/child')
16-
17-
import { spawn } from '@socketsecurity/lib/process/spawn/child'
18-
import { getCodeCoverage } from '@socketsecurity/lib/cover/code'
15+
// Mock via the src/ relative path so vitest intercepts the same
16+
// module instance that src/cover/code.ts imports (which uses
17+
// relative paths internally). Package-specifier mocks don't survive
18+
// vitest's threaded-pool dedup gaps.
19+
vi.mock('../../../src/process/spawn/child')
20+
21+
import { spawn } from '../../../src/process/spawn/child'
22+
import { getCodeCoverage } from '../../../src/cover/code'
1923

2024
let tmpDir: string
2125

test/unit/cover/type.test.mts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212

1313
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
1414

15-
// Mock spawn at the package specifier — src resolution is enabled in
16-
// vitest.config.mts via the 'source' export condition.
17-
vi.mock('@socketsecurity/lib/process/spawn/child')
15+
// Mock via the src/ relative path so vitest intercepts the same
16+
// module instance that src/cover/type.ts imports (which uses
17+
// relative paths internally). Package-specifier mocks don't survive
18+
// vitest's threaded-pool dedup gaps. See:
19+
// test/unit/releases/*.test.mts for the canonical pattern.
20+
vi.mock('../../../src/process/spawn/child')
1821

19-
import { spawn } from '@socketsecurity/lib/process/spawn/child'
20-
import { getTypeCoverage } from '@socketsecurity/lib/cover/type'
22+
import { spawn } from '../../../src/process/spawn/child'
23+
import { getTypeCoverage } from '../../../src/cover/type'
2124

2225
describe('cover/type', () => {
2326
beforeEach(() => {

0 commit comments

Comments
 (0)