-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi.test.ts
More file actions
31 lines (26 loc) · 1.01 KB
/
api.test.ts
File metadata and controls
31 lines (26 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { describe, expect, test, mock, afterEach, beforeEach, spyOn } from 'bun:test';
import fs from 'fs';
import { loadSession } from '../src/api';
describe('api.ts loadSession', () => {
let originalConsoleError: any;
let existsSyncSpy: any;
let readFileSyncSpy: any;
beforeEach(() => {
originalConsoleError = console.error;
console.error = mock(() => {});
// Use spyOn to mock specific fs methods
existsSyncSpy = spyOn(fs, 'existsSync').mockReturnValue(true);
readFileSyncSpy = spyOn(fs, 'readFileSync').mockReturnValue('{ "invalid": json ');
});
afterEach(() => {
console.error = originalConsoleError;
existsSyncSpy.mockRestore();
readFileSyncSpy.mockRestore();
});
test('loadSession throws error on JSON parse failure', async () => {
// Assert that the Promise rejects with a SyntaxError (JSON parse error)
await expect(loadSession()).rejects.toThrow(SyntaxError);
// Verify that console.error was called
expect(console.error).toHaveBeenCalled();
});
});