-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.test.ts
More file actions
75 lines (60 loc) · 2.23 KB
/
Copy pathlogger.test.ts
File metadata and controls
75 lines (60 loc) · 2.23 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { createLogger, LogLevel, setLogLevel, addTransport } from './logger';
describe('Logger', () => {
beforeEach(() => {
vi.clearAllMocks();
setLogLevel(LogLevel.DEBUG);
});
it('should log at different levels', () => {
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
const logger = createLogger('test');
logger.debug('debug message');
logger.info('info message');
logger.warn('warn message');
expect(consoleSpy).toHaveBeenCalledTimes(3);
const output = consoleSpy.mock.calls.map((c) => c[0]).join(' ');
expect(output).toContain('DEBUG');
expect(output).toContain('INFO');
expect(output).toContain('WARN');
});
it('should respect log levels', () => {
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
setLogLevel(LogLevel.WARN);
const logger = createLogger('test');
logger.debug('should not show');
logger.info('should not show');
logger.warn('should show');
expect(consoleSpy).toHaveBeenCalledTimes(1);
expect(consoleSpy.mock.calls[0][0]).toContain('WARN');
});
it('should support custom transports', () => {
const transport = vi.fn();
addTransport(transport);
const logger = createLogger('test');
logger.info('hello transport', { foo: 'bar' });
expect(transport).toHaveBeenCalledWith(
expect.objectContaining({
level: 'INFO',
message: 'hello transport',
data: { foo: 'bar' },
namespace: 'test',
})
);
});
it('should measure execution time', async () => {
const logger = createLogger('test');
const result = await logger.measure('task', async () => {
await new Promise((r) => setTimeout(r, 10));
return 'done';
});
expect(result).toBe('done');
});
it('should be callable as a function (backward compatibility)', () => {
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
const logger = createLogger('test');
logger('classic message');
expect(consoleSpy).toHaveBeenCalled();
expect(consoleSpy.mock.calls[0][0]).toContain('DEBUG');
expect(consoleSpy.mock.calls[0][0]).toContain('classic message');
});
});