Skip to content

Commit 12cd726

Browse files
committed
fix(test): use tmpdir for patch discover test to avoid spawn failures
The test "should show error when no node_modules directory found" was using a nonexistent-dir path as cwd. On Unix systems, spawn() fails with ENOENT before the CLI even starts when cwd doesn't exist. Fix: Create a temporary directory using mkdtemp(), which guarantees: - Directory exists (spawn won't fail) - No node_modules present (test condition is valid) - Isolated test environment - Automatic cleanup in finally block This eliminates the Unix flaky test failure.
1 parent 5734a2e commit 12cd726

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

packages/cli/src/commands/patch/cmd-patch-discover.test.mts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { promises as fs } from 'node:fs'
2+
import os from 'node:os'
23
import path from 'node:path'
34

45
import { afterEach, describe, expect } from 'vitest'
@@ -50,12 +51,19 @@ describe('socket patch discover', async () => {
5051
['patch', 'discover', FLAG_CONFIG, '{"apiToken":"fake-token"}'],
5152
'should show error when no node_modules directory found',
5253
async cmd => {
53-
const { code, stderr, stdout } = await spawnSocketCli(binCliPath, cmd, {
54-
cwd: path.join(fixtureBaseDir, 'nonexistent-dir'),
55-
})
56-
const output = stdout + stderr
57-
expect(output).toContain('No node_modules directory found')
58-
expect(code, 'should exit with non-zero code').not.toBe(0)
54+
// Create a temporary directory without node_modules.
55+
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'socket-test-'))
56+
try {
57+
const { code, stderr, stdout } = await spawnSocketCli(binCliPath, cmd, {
58+
cwd: tmpDir,
59+
})
60+
const output = stdout + stderr
61+
expect(output).toContain('No node_modules directory found')
62+
expect(code, 'should exit with non-zero code').not.toBe(0)
63+
} finally {
64+
// Clean up temporary directory.
65+
await fs.rm(tmpDir, { force: true, recursive: true })
66+
}
5967
},
6068
)
6169

0 commit comments

Comments
 (0)