Skip to content

Commit 97fc137

Browse files
committed
test: fix flaky shadow npm and path-resolve tests in CI
- Add isDebug mock to npm-base.test.mts and install.test.mts to ensure --loglevel args are consistently added (isDebug() was returning true in CI due to environment variables) - Use hoisted mocks for whichRealSync in path-resolve.test.mts to ensure reliable mock behavior in CI (dynamic import pattern was failing)
1 parent c572023 commit 97fc137

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

packages/cli/test/unit/shadow/npm-base.test.mts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ vi.mock('@socketsecurity/lib/constants/node', () => ({
117117
supportsNodePermissionFlag: vi.fn(() => true),
118118
}))
119119

120+
// Mock isDebug to always return false so --loglevel args are added.
121+
vi.mock('@socketsecurity/lib/debug', () => ({
122+
isDebug: vi.fn(() => false),
123+
}))
124+
120125
describe('shadowNpmBase', () => {
121126
const mockSpawnResult = Promise.resolve({
122127
success: true,

packages/cli/test/unit/shadow/npm/install.test.mts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ vi.mock('../../../../src/constants/cli.mts', () => ({
9595
FLAG_LOGLEVEL: '--loglevel',
9696
}))
9797

98+
// Mock isDebug to always return false so --loglevel args are added.
99+
vi.mock('@socketsecurity/lib/debug', () => ({
100+
isDebug: vi.fn(() => false),
101+
}))
102+
98103
describe('shadowNpmInstall', () => {
99104
const mockProcess = {
100105
send: vi.fn(),

packages/cli/test/unit/utils/fs/path-resolve.test.mts

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,19 @@ import { createTestWorkspace } from '../../../helpers/workspace-helper.mts'
3737

3838
const PACKAGE_JSON = 'package.json'
3939

40+
// Hoisted mocks for better CI reliability.
41+
const mockWhichRealSync = vi.hoisted(() => vi.fn())
42+
const mockResolveBinPathSync = vi.hoisted(() => vi.fn((p: string) => p))
43+
4044
// Mock dependencies for new tests.
4145
vi.mock('@socketsecurity/lib/bin', async () => {
4246
const actual = await vi.importActual<
4347
typeof import('@socketsecurity/lib/bin')
4448
>('@socketsecurity/lib/bin')
4549
return {
4650
...actual,
47-
resolveBinPathSync: vi.fn(p => p),
48-
whichRealSync: vi.fn(),
51+
resolveBinPathSync: mockResolveBinPathSync,
52+
whichRealSync: mockWhichRealSync,
4953
}
5054
})
5155

@@ -382,11 +386,8 @@ describe('Path Resolve', () => {
382386
vi.clearAllMocks()
383387
})
384388

385-
it('finds bin path when available', async () => {
386-
const { whichRealSync } = vi.mocked(
387-
await import('@socketsecurity/lib/bin'),
388-
)
389-
whichRealSync.mockReturnValue(['/usr/local/bin/npm'])
389+
it('finds bin path when available', () => {
390+
mockWhichRealSync.mockReturnValue(['/usr/local/bin/npm'])
390391

391392
const result = findBinPathDetailsSync('npm')
392393

@@ -400,10 +401,7 @@ describe('Path Resolve', () => {
400401
it('handles shadowed bin paths', async () => {
401402
const constants = await import('../../../../src/constants.mts')
402403
const shadowBinPath = constants.default.shadowBinPath
403-
const { whichRealSync } = vi.mocked(
404-
await import('@socketsecurity/lib/bin'),
405-
)
406-
whichRealSync.mockReturnValue([
404+
mockWhichRealSync.mockReturnValue([
407405
`${shadowBinPath}/npm`,
408406
'/usr/local/bin/npm',
409407
])
@@ -417,11 +415,8 @@ describe('Path Resolve', () => {
417415
})
418416
})
419417

420-
it('handles no bin path found', async () => {
421-
const { whichRealSync } = vi.mocked(
422-
await import('@socketsecurity/lib/bin'),
423-
)
424-
whichRealSync.mockReturnValue(null)
418+
it('handles no bin path found', () => {
419+
mockWhichRealSync.mockReturnValue(null)
425420

426421
const result = findBinPathDetailsSync('nonexistent')
427422

@@ -432,11 +427,8 @@ describe('Path Resolve', () => {
432427
})
433428
})
434429

435-
it('handles empty array result', async () => {
436-
const { whichRealSync } = vi.mocked(
437-
await import('@socketsecurity/lib/bin'),
438-
)
439-
whichRealSync.mockReturnValue([])
430+
it('handles empty array result', () => {
431+
mockWhichRealSync.mockReturnValue([])
440432

441433
const result = findBinPathDetailsSync('npm')
442434

@@ -447,11 +439,8 @@ describe('Path Resolve', () => {
447439
})
448440
})
449441

450-
it('handles single string result', async () => {
451-
const { whichRealSync } = vi.mocked(
452-
await import('@socketsecurity/lib/bin'),
453-
)
454-
whichRealSync.mockReturnValue('/usr/local/bin/npm' as any)
442+
it('handles single string result', () => {
443+
mockWhichRealSync.mockReturnValue('/usr/local/bin/npm' as any)
455444

456445
const result = findBinPathDetailsSync('npm')
457446

@@ -465,10 +454,7 @@ describe('Path Resolve', () => {
465454
it('handles only shadow bin in path', async () => {
466455
const constants = await import('../../../../src/constants.mts')
467456
const shadowBinPath = constants.default.shadowBinPath
468-
const { whichRealSync } = vi.mocked(
469-
await import('@socketsecurity/lib/bin'),
470-
)
471-
whichRealSync.mockReturnValue([`${shadowBinPath}/npm`])
457+
mockWhichRealSync.mockReturnValue([`${shadowBinPath}/npm`])
472458

473459
const result = findBinPathDetailsSync('npm')
474460

0 commit comments

Comments
 (0)