Skip to content

Commit 4b2a94c

Browse files
committed
test(cli): fix flaky path-resolve tests on Windows
Fix 4 Windows-specific test failures by normalizing paths for cross-platform compatibility. Tests were comparing Unix-style paths against Windows paths. Changes: - Use normalizePath() in mock implementations to handle path separators - Use normalizePath() when asserting expected results - Ensures tests work on both Unix (forward slash) and Windows (backslash) Fixes flaky tests in findNpmDirPathSync(): - "finds npm directory in lib/node_modules structure" - "finds npm directory with node_modules in current path" - "finds npm directory with node_modules in parent path" - "handles nvm directory structure" All 21 tests in path-resolve.test.mts now pass on all platforms.
1 parent e7ba2f2 commit 4b2a94c

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ describe('Path Resolve', () => {
473473
const { isDirSync } = vi.mocked(await import('@socketsecurity/lib/fs'))
474474

475475
isDirSync.mockImplementation(p => {
476-
const pathStr = String(p)
476+
const pathStr = normalizePath(String(p))
477477
if (pathStr.includes('lib/node_modules/npm')) {
478478
return true
479479
}
@@ -485,42 +485,42 @@ describe('Path Resolve', () => {
485485

486486
const result = findNpmDirPathSync('/usr/local/bin/npm')
487487

488-
expect(result).toBe('/usr/local/bin/npm/lib/node_modules/npm')
488+
expect(normalizePath(result)).toBe(normalizePath('/usr/local/bin/npm/lib/node_modules/npm'))
489489
})
490490

491491
it('finds npm directory with node_modules in current path', async () => {
492492
const { isDirSync } = vi.mocked(await import('@socketsecurity/lib/fs'))
493493

494494
isDirSync.mockImplementation(p => {
495-
const pathStr = String(p)
496-
if (pathStr === '/usr/local/npm/node_modules') {
495+
const pathStr = normalizePath(String(p))
496+
if (pathStr === normalizePath('/usr/local/npm/node_modules')) {
497497
return true
498498
}
499499
return false
500500
})
501501

502502
const result = findNpmDirPathSync('/usr/local/npm')
503503

504-
expect(result).toBe('/usr/local/npm')
504+
expect(normalizePath(result)).toBe(normalizePath('/usr/local/npm'))
505505
})
506506

507507
it('finds npm directory with node_modules in parent path', async () => {
508508
const { isDirSync } = vi.mocked(await import('@socketsecurity/lib/fs'))
509509

510510
isDirSync.mockImplementation(p => {
511-
const pathStr = String(p)
512-
if (pathStr === '/usr/local/npm/node_modules') {
511+
const pathStr = normalizePath(String(p))
512+
if (pathStr === normalizePath('/usr/local/npm/node_modules')) {
513513
return false
514514
}
515-
if (pathStr === '/usr/local/node_modules') {
515+
if (pathStr === normalizePath('/usr/local/node_modules')) {
516516
return true
517517
}
518518
return false
519519
})
520520

521521
const result = findNpmDirPathSync('/usr/local/npm')
522522

523-
expect(result).toBe('/usr/local')
523+
expect(normalizePath(result)).toBe(normalizePath('/usr/local'))
524524
})
525525

526526
it('returns undefined when no npm directory found', async () => {
@@ -537,7 +537,7 @@ describe('Path Resolve', () => {
537537
const { isDirSync } = vi.mocked(await import('@socketsecurity/lib/fs'))
538538

539539
isDirSync.mockImplementation(p => {
540-
const pathStr = String(p)
540+
const pathStr = normalizePath(String(p))
541541
if (pathStr.includes('.nvm') && pathStr.endsWith('/node_modules')) {
542542
return true
543543
}
@@ -548,7 +548,7 @@ describe('Path Resolve', () => {
548548
'/Users/user/.nvm/versions/node/v18.0.0/bin/npm',
549549
)
550550

551-
expect(result).toBe('/Users/user/.nvm/versions/node/v18.0.0/bin/npm')
551+
expect(normalizePath(result)).toBe(normalizePath('/Users/user/.nvm/versions/node/v18.0.0/bin/npm'))
552552
})
553553
})
554554
})

0 commit comments

Comments
 (0)