Skip to content

Commit a513983

Browse files
committed
test: improve test coverage for CLI and logger
1 parent 1cefdf4 commit a513983

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎test/unit/cli.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { CLI } from '../../src/cli';
22
import path from 'path';
33
import { serve } from '../../src/server/server';
44
import { resolveGlobPatterns } from '../../src/utils/glob';
5+
import { logger } from '../../src/utils/logger';
56

67
// Mock fs first, before any other imports that might use it
78
let mockExistingFiles: string[] = [];
@@ -53,11 +54,13 @@ describe('cli', () => {
5354
let originalArgv: string[];
5455
const mockOpen = jest.fn();
5556
let mockServe: jest.Mock;
57+
const mockLogger = logger as jest.Mocked<typeof logger>;
5658

5759
beforeEach(() => {
5860
cli = new CLI();
5961
originalArgv = process.argv;
6062
mockServe = serve as jest.Mock;
63+
process.exitCode = undefined;
6164
// Reset the mock result for existsSync before each test
6265
setExistingFiles([]); // Default to empty for safety
6366
jest.clearAllMocks();
@@ -155,6 +158,31 @@ describe('cli', () => {
155158
});
156159
});
157160

161+
it('should log the server URL without opening the browser when --no-open is specified', () => {
162+
setExistsSyncResult(false);
163+
process.argv = ['node', 'cli.ts', '--no-open', '--host', '0.0.0.0', '.'];
164+
165+
return cli.run()
166+
.then(() => {
167+
expect(mockServe).toHaveBeenCalledWith({ directory: path.resolve('.') }, 8521, '0.0.0.0', false);
168+
expect(mockLogger.log).toHaveBeenCalledWith('CLI', '🌐 Server running at http://localhost:8521');
169+
expect(mockOpen).not.toHaveBeenCalled();
170+
});
171+
});
172+
173+
it('should log an error and set exitCode when serve fails', () => {
174+
setExistsSyncResult(false);
175+
mockServe.mockRejectedValueOnce(new Error('boom'));
176+
process.argv = ['node', 'cli.ts', '.'];
177+
178+
return cli.run()
179+
.then(() => {
180+
expect(mockLogger.error).toHaveBeenCalledWith('❌ Failed to start server: boom');
181+
expect(process.exitCode).toBe(1);
182+
expect(mockOpen).not.toHaveBeenCalled();
183+
});
184+
});
185+
158186
it('should call resolveGlobPatterns when --glob option is provided', () => {
159187
setExistsSyncResult(false);
160188
process.argv = ['node', 'cli.ts', './docs', '--glob', '*.md'];

‎test/unit/utils/logger.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
const flushAsyncWork = () => new Promise<void>((resolve) => setImmediate(resolve));
2+
const stripAnsi = (message: string) => {
3+
const escape = String.fromCharCode(27);
4+
return message.replace(new RegExp(`${escape}\\[[0-9;]*m`, 'g'), '');
5+
};
26

37
const loadLogger = () => {
48
jest.unmock('../../../src/utils/logger');
@@ -53,7 +57,7 @@ describe('logger', () => {
5357
logger.log(tag as never, 'plain message');
5458
await flushAsyncWork();
5559

56-
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining(`${prefix} plain message`));
60+
expect(stripAnsi(logSpy.mock.calls[0][0] as string)).toContain(`${prefix} plain message`);
5761
});
5862

5963
it('formats URLs as clickable links and forwards extra arguments', async () => {

0 commit comments

Comments
 (0)