Skip to content

Commit 9190b1c

Browse files
committed
test: address review feedback on mock removal changes
- Remove redundant CACHE_DIR alias, use tmpDir directly - Add force:true to afterAll rmSync for robustness - Wrap chmod test in try/finally to prevent leaked read-only state - Remove vacuous conditional assertion in resolve-ui-dist-dir - Switch get-trace to beforeEach/afterEach for test isolation consistency
1 parent f6ff3f9 commit 9190b1c

3 files changed

Lines changed: 18 additions & 26 deletions

File tree

src/cli/__tests__/update-notifier.test.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ vi.mock('../commands/update/action.js', async importOriginal => {
3131
return { ...actual, fetchLatestVersion: mockFetchLatestVersion };
3232
});
3333

34-
const CACHE_DIR = tmpDir;
3534
const CACHE_FILE = join(tmpDir, 'update-check.json');
3635

3736
describe('checkForUpdate', () => {
3837
beforeEach(() => {
3938
vi.spyOn(Date, 'now').mockReturnValue(1708646400000);
4039
try {
41-
rmSync(CACHE_DIR, { recursive: true });
40+
rmSync(tmpDir, { recursive: true, force: true });
4241
} catch {}
4342
});
4443

@@ -48,7 +47,7 @@ describe('checkForUpdate', () => {
4847
});
4948

5049
afterAll(() => {
51-
rmSync(tmpDir, { recursive: true });
50+
rmSync(tmpDir, { recursive: true, force: true });
5251
});
5352

5453
it('fetches from registry when no cache exists', async () => {
@@ -61,7 +60,7 @@ describe('checkForUpdate', () => {
6160
});
6261

6362
it('uses cache when last check was less than 24 hours ago', async () => {
64-
await mkdir(CACHE_DIR, { recursive: true });
63+
await mkdir(tmpDir, { recursive: true });
6564
await writeFile(CACHE_FILE, JSON.stringify({ lastCheck: 1708646400000 - 1000, latestVersion: '2.0.0' }), 'utf-8');
6665

6766
const result = await checkForUpdate();
@@ -71,7 +70,7 @@ describe('checkForUpdate', () => {
7170
});
7271

7372
it('fetches from registry when cache is expired', async () => {
74-
await mkdir(CACHE_DIR, { recursive: true });
73+
await mkdir(tmpDir, { recursive: true });
7574
await writeFile(
7675
CACHE_FILE,
7776
JSON.stringify({ lastCheck: 1708646400000 - 25 * 60 * 60 * 1000, latestVersion: '1.5.0' }),
@@ -119,7 +118,7 @@ describe('checkForUpdate', () => {
119118
});
120119

121120
it('returns null on cache parse error and fetch error', async () => {
122-
await mkdir(CACHE_DIR, { recursive: true });
121+
await mkdir(tmpDir, { recursive: true });
123122
await writeFile(CACHE_FILE, 'invalid json', 'utf-8');
124123
mockFetchLatestVersion.mockRejectedValue(new Error('network error'));
125124

@@ -129,18 +128,20 @@ describe('checkForUpdate', () => {
129128
});
130129

131130
it('succeeds even when cache write fails', async () => {
132-
await mkdir(CACHE_DIR, { recursive: true });
131+
await mkdir(tmpDir, { recursive: true });
133132
await writeFile(CACHE_FILE, '', 'utf-8');
134133
const { chmod } = await import('fs/promises');
135-
await chmod(CACHE_DIR, 0o444);
134+
await chmod(tmpDir, 0o444);
136135

137-
mockFetchLatestVersion.mockResolvedValue('2.0.0');
138-
139-
const result = await checkForUpdate();
136+
try {
137+
mockFetchLatestVersion.mockResolvedValue('2.0.0');
140138

141-
expect(result).toEqual({ updateAvailable: true, latestVersion: '2.0.0' });
139+
const result = await checkForUpdate();
142140

143-
await chmod(CACHE_DIR, 0o755);
141+
expect(result).toEqual({ updateAvailable: true, latestVersion: '2.0.0' });
142+
} finally {
143+
await chmod(tmpDir, 0o755);
144+
}
144145
});
145146
});
146147

src/cli/operations/dev/web-ui/__tests__/resolve-ui-dist-dir.test.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,4 @@ describe('resolveUIDistDir', () => {
3939

4040
expect(resolveUIDistDir()).toBe(tmpDir);
4141
});
42-
43-
it('returns a bundled candidate when no env var is set and bundled path exists', () => {
44-
const result = resolveUIDistDir();
45-
// If a bundled candidate has index.html, it should be returned
46-
if (result) {
47-
expect(result).toContain('dist-assets');
48-
}
49-
});
5042
});

src/cli/operations/traces/__tests__/get-trace.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { FetchTraceRecordsOptions } from '../types';
33
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
44
import { tmpdir } from 'node:os';
55
import { join } from 'node:path';
6-
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
6+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
77

88
const { mockSend } = vi.hoisted(() => ({
99
mockSend: vi.fn(),
@@ -181,16 +181,15 @@ describe('fetchTraceRecords', () => {
181181
describe('getTrace', () => {
182182
let tmpDir: string;
183183

184-
beforeAll(() => {
184+
beforeEach(() => {
185185
tmpDir = mkdtempSync(join(tmpdir(), 'get-trace-test-'));
186186
});
187187

188-
afterAll(() => {
188+
afterEach(() => {
189+
vi.clearAllMocks();
189190
rmSync(tmpDir, { recursive: true, force: true });
190191
});
191192

192-
afterEach(() => vi.clearAllMocks());
193-
194193
it('calls fetchTraceRecords and writes result to disk', async () => {
195194
mockSend.mockResolvedValueOnce({ queryId: 'q-1' }).mockResolvedValueOnce({
196195
status: 'Complete',

0 commit comments

Comments
 (0)