Skip to content

Commit 66c3764

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 66c3764

3 files changed

Lines changed: 29 additions & 45 deletions

File tree

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

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,37 @@
11
import { type UpdateCheckResult, checkForUpdate, printUpdateNotification } from '../update-notifier.js';
22
import { rmSync } from 'fs';
3-
import { mkdir, readFile, writeFile } from 'fs/promises';
3+
import { chmod, mkdir, readFile, writeFile } from 'fs/promises';
44
import { join } from 'path';
55
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
66

7-
const tmpDir = vi.hoisted(() => {
8-
/* eslint-disable @typescript-eslint/no-require-imports */
9-
const fs = require('fs');
10-
const os = require('os');
11-
const path = require('path');
12-
/* eslint-enable @typescript-eslint/no-require-imports */
13-
return fs.mkdtempSync(path.join(os.tmpdir(), 'update-notifier-test-'));
14-
});
7+
/* eslint-disable @typescript-eslint/no-require-imports -- vi.hoisted runs before ESM imports are available */
8+
const tmpDir = vi.hoisted(() =>
9+
require('fs').mkdtempSync(require('path').join(require('os').tmpdir(), 'update-notifier-test-'))
10+
);
11+
/* eslint-enable @typescript-eslint/no-require-imports */
12+
13+
const mockFetchLatestVersion = vi.hoisted(() => vi.fn());
1514

1615
vi.mock('../../lib/schemas/io/global-config.js', () => ({
1716
GLOBAL_CONFIG_DIR: tmpDir,
1817
}));
1918

20-
vi.mock('../constants.js', async importOriginal => {
21-
const actual = await importOriginal<typeof import('../constants.js')>();
22-
return { ...actual, PACKAGE_VERSION: '1.0.0' };
23-
});
24-
25-
const { mockFetchLatestVersion } = vi.hoisted(() => ({
26-
mockFetchLatestVersion: vi.fn(),
19+
vi.mock('../constants.js', async importOriginal => ({
20+
...(await importOriginal<typeof import('../constants.js')>()),
21+
PACKAGE_VERSION: '1.0.0',
2722
}));
2823

2924
vi.mock('../commands/update/action.js', async importOriginal => {
3025
const actual = await importOriginal<typeof import('../commands/update/action.js')>();
3126
return { ...actual, fetchLatestVersion: mockFetchLatestVersion };
3227
});
3328

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

3731
describe('checkForUpdate', () => {
3832
beforeEach(() => {
3933
vi.spyOn(Date, 'now').mockReturnValue(1708646400000);
40-
try {
41-
rmSync(CACHE_DIR, { recursive: true });
42-
} catch {}
34+
rmSync(tmpDir, { recursive: true, force: true });
4335
});
4436

4537
afterEach(() => {
@@ -48,7 +40,7 @@ describe('checkForUpdate', () => {
4840
});
4941

5042
afterAll(() => {
51-
rmSync(tmpDir, { recursive: true });
43+
rmSync(tmpDir, { recursive: true, force: true });
5244
});
5345

5446
it('fetches from registry when no cache exists', async () => {
@@ -61,7 +53,7 @@ describe('checkForUpdate', () => {
6153
});
6254

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

6759
const result = await checkForUpdate();
@@ -71,7 +63,7 @@ describe('checkForUpdate', () => {
7163
});
7264

7365
it('fetches from registry when cache is expired', async () => {
74-
await mkdir(CACHE_DIR, { recursive: true });
66+
await mkdir(tmpDir, { recursive: true });
7567
await writeFile(
7668
CACHE_FILE,
7769
JSON.stringify({ lastCheck: 1708646400000 - 25 * 60 * 60 * 1000, latestVersion: '1.5.0' }),
@@ -119,7 +111,7 @@ describe('checkForUpdate', () => {
119111
});
120112

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

@@ -129,18 +121,19 @@ describe('checkForUpdate', () => {
129121
});
130122

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

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

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

143-
await chmod(CACHE_DIR, 0o755);
133+
expect(result).toEqual({ updateAvailable: true, latestVersion: '2.0.0' });
134+
} finally {
135+
await chmod(tmpDir, 0o755);
136+
}
144137
});
145138
});
146139

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)