test: enhance ClaudeUsageService tests with promise handling and exit callback

- Added an `afterEach` hook to clean up after tests in `claude-usage-service.test.ts`.
- Updated the mock for `onExit` to include an exit callback, ensuring proper handling of process termination.
- Modified the `fetchUsageData` test to await the promise resolution, preventing unhandled promise rejections.

These changes improve the reliability and robustness of the unit tests for the ClaudeUsageService.
This commit is contained in:
Kacper
2025-12-21 23:18:49 +01:00
parent 26236d3d5b
commit 0c59add31f
2 changed files with 18 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { ClaudeUsageService } from '@/services/claude-usage-service.js';
import { spawn } from 'child_process';
import * as pty from 'node-pty';
@@ -563,18 +563,21 @@ Resets in 2h
const mockOutput = 'Current session\n65% left';
let dataCallback: Function | undefined;
let exitCallback: Function | undefined;
const mockPty = {
onData: vi.fn((callback: Function) => {
dataCallback = callback;
}),
onExit: vi.fn(),
onExit: vi.fn((callback: Function) => {
exitCallback = callback;
}),
write: vi.fn(),
kill: vi.fn(),
};
vi.mocked(pty.spawn).mockReturnValue(mockPty as any);
windowsService.fetchUsageData();
const promise = windowsService.fetchUsageData();
// Simulate seeing usage data
dataCallback!(mockOutput);
@@ -584,6 +587,10 @@ Resets in 2h
expect(mockPty.write).toHaveBeenCalledWith('\x1b');
// Complete the promise to avoid unhandled rejection
exitCallback!({ exitCode: 0 });
await promise;
vi.useRealTimers();
});