Skip to content

Commit 03a9edf

Browse files
🧪 Add missing tests for loadSession parsing failure in src/api.ts (#20)
- Mocked fs.existsSync to return true - Mocked fs.readFileSync to return an invalid JSON string - Tested that loadSession throws a JSON parse error (SyntaxError) - Verified that console.error is called during the failure - Handled mock restoration correctly using spyOn to avoid interfering with other tests Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com>
1 parent ae592d0 commit 03a9edf

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

‎bun.lock‎

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/api.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, expect, test, mock, afterEach, beforeEach, spyOn } from 'bun:test';
2+
import fs from 'fs';
3+
import { loadSession } from '../src/api';
4+
5+
describe('api.ts loadSession', () => {
6+
let originalConsoleError: any;
7+
let existsSyncSpy: any;
8+
let readFileSyncSpy: any;
9+
10+
beforeEach(() => {
11+
originalConsoleError = console.error;
12+
console.error = mock(() => {});
13+
14+
// Use spyOn to mock specific fs methods
15+
existsSyncSpy = spyOn(fs, 'existsSync').mockReturnValue(true);
16+
readFileSyncSpy = spyOn(fs, 'readFileSync').mockReturnValue('{ "invalid": json ');
17+
});
18+
19+
afterEach(() => {
20+
console.error = originalConsoleError;
21+
existsSyncSpy.mockRestore();
22+
readFileSyncSpy.mockRestore();
23+
});
24+
25+
test('loadSession throws error on JSON parse failure', async () => {
26+
// Assert that the Promise rejects with a SyntaxError (JSON parse error)
27+
await expect(loadSession()).rejects.toThrow(SyntaxError);
28+
// Verify that console.error was called
29+
expect(console.error).toHaveBeenCalled();
30+
});
31+
});

0 commit comments

Comments
 (0)