Skip to content

Commit 82c5e43

Browse files
committed
test(optimize): clean up pnpm fixture artifacts after tests
The pnpm v8/v9 optimize fixture tests run `pnpm install` inside the fixture directories during beforeEach to provision the specific pnpm binary version. The existing afterEach hooks only reset tracked files (`git checkout HEAD -- .`) but left untracked `node_modules/` and `.cache/` directories behind, polluting the working tree. This caused two problems: 1. Running `git add -A` (or relying on pre-commit hooks that do) would accidentally stage thousands of untracked fixture artifacts. 2. The pnpm9 fixture install was silently failing (swallowed by `try/catch` + `stdio: 'ignore'`), so pnpm9 tests fell back to the system pnpm rather than testing against pnpm v9. Fix: - Import `trash` and remove `node_modules/` + `.cache/` in both beforeEach (to clean up after prior aborted runs) and afterEach (to keep the fixture clean going forward). - Use `Promise.all` for parallel cleanup to avoid `no-await-in-loop` lint warnings. All 4 pnpm-version tests continue to pass and fixtures are now clean after each run.
1 parent b6c3411 commit 82c5e43

1 file changed

Lines changed: 35 additions & 4 deletions

File tree

src/commands/optimize/cmd-optimize-pnpm-versions.test.mts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync } from 'node:fs'
22
import path from 'node:path'
33

4+
import trash from 'trash'
45
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
56

67
import { JsonContent } from '@socketsecurity/registry/lib/fs'
@@ -27,13 +28,21 @@ describe('socket optimize - pnpm versions', { timeout: 60_000 }, async () => {
2728
const pnpm8BinPath = path.join(pnpm8FixtureDir, 'node_modules', '.bin')
2829

2930
beforeEach(async () => {
30-
// Reset fixtures to their committed state (package.json and pnpm-lock.yaml).
31+
// Reset fixtures to their committed state (package.json and pnpm-lock.yaml)
32+
// and remove untracked install artifacts left by prior runs.
3133
try {
3234
await spawn('git', ['checkout', 'HEAD', '--', '.'], {
3335
cwd: pnpm8FixtureDir,
3436
stdio: 'ignore',
3537
})
3638
} catch {}
39+
const staleDirs = [
40+
path.join(pnpm8FixtureDir, 'node_modules'),
41+
path.join(pnpm8FixtureDir, '.cache'),
42+
]
43+
await Promise.all(
44+
staleDirs.filter(dir => existsSync(dir)).map(dir => trash(dir)),
45+
)
3746
// Ensure pnpm v8 is installed in the fixture.
3847
// Skip if pnpm is not available globally (e.g., Windows CI).
3948
try {
@@ -54,13 +63,20 @@ describe('socket optimize - pnpm versions', { timeout: 60_000 }, async () => {
5463
})
5564

5665
afterEach(async () => {
57-
// Reset fixtures to their committed state after each test.
66+
// Reset tracked fixture files and remove install artifacts.
5867
try {
5968
await spawn('git', ['checkout', 'HEAD', '--', '.'], {
6069
cwd: pnpm8FixtureDir,
6170
stdio: 'ignore',
6271
})
6372
} catch {}
73+
const staleDirs = [
74+
path.join(pnpm8FixtureDir, 'node_modules'),
75+
path.join(pnpm8FixtureDir, '.cache'),
76+
]
77+
await Promise.all(
78+
staleDirs.filter(dir => existsSync(dir)).map(dir => trash(dir)),
79+
)
6480
})
6581

6682
it(
@@ -161,13 +177,21 @@ describe('socket optimize - pnpm versions', { timeout: 60_000 }, async () => {
161177
const pnpm9BinPath = path.join(pnpm9FixtureDir, 'node_modules', '.bin')
162178

163179
beforeEach(async () => {
164-
// Reset fixtures to their committed state (package.json and pnpm-lock.yaml).
180+
// Reset fixtures to their committed state (package.json and pnpm-lock.yaml)
181+
// and remove untracked install artifacts left by prior runs.
165182
try {
166183
await spawn('git', ['checkout', 'HEAD', '--', '.'], {
167184
cwd: pnpm9FixtureDir,
168185
stdio: 'ignore',
169186
})
170187
} catch {}
188+
const staleDirs = [
189+
path.join(pnpm9FixtureDir, 'node_modules'),
190+
path.join(pnpm9FixtureDir, '.cache'),
191+
]
192+
await Promise.all(
193+
staleDirs.filter(dir => existsSync(dir)).map(dir => trash(dir)),
194+
)
171195
// Ensure pnpm v9 is installed in the fixture.
172196
// Skip if pnpm is not available globally (e.g., Windows CI).
173197
try {
@@ -188,13 +212,20 @@ describe('socket optimize - pnpm versions', { timeout: 60_000 }, async () => {
188212
})
189213

190214
afterEach(async () => {
191-
// Reset fixtures to their committed state after each test.
215+
// Reset tracked fixture files and remove install artifacts.
192216
try {
193217
await spawn('git', ['checkout', 'HEAD', '--', '.'], {
194218
cwd: pnpm9FixtureDir,
195219
stdio: 'ignore',
196220
})
197221
} catch {}
222+
const staleDirs = [
223+
path.join(pnpm9FixtureDir, 'node_modules'),
224+
path.join(pnpm9FixtureDir, '.cache'),
225+
]
226+
await Promise.all(
227+
staleDirs.filter(dir => existsSync(dir)).map(dir => trash(dir)),
228+
)
198229
})
199230

200231
it(

0 commit comments

Comments
 (0)