Skip to content

Commit 8fb9917

Browse files
committed
test(logger): create test
1 parent ca8335f commit 8fb9917

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

__tests__/logger.service.test.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { jest } from '@jest/globals';
2+
3+
const writeFileSyncMock = jest.fn();
4+
jest.unstable_mockModule('fs', () => {
5+
return {
6+
writeFileSync: writeFileSyncMock,
7+
default: jest.fn(),
8+
};
9+
});
10+
11+
let osTmpPath = '/tmpDir';
12+
jest.unstable_mockModule('os', () => {
13+
return {
14+
tmpdir: () => osTmpPath,
15+
};
16+
});
17+
18+
const LoggerServiceConstructor = //@ts-ignore
19+
(await import('../src/services/logger.service.js')).LoggerService;
20+
class LoggerService extends LoggerServiceConstructor {}
21+
22+
describe('LoggerService', () => {
23+
let logger: LoggerService;
24+
let fakeTime = new Date('2026-01-01');
25+
let fakeTimeEpox = fakeTime.getTime();
26+
27+
beforeEach(() => {
28+
logger = new LoggerService();
29+
jest.useFakeTimers().setSystemTime(fakeTime);
30+
});
31+
32+
describe('add to log (info, error)', () => {
33+
it('should add the message to the log with the correct type and timestamp', () => {
34+
expect(logger.get()).toEqual([]);
35+
logger.info('Sample message1');
36+
logger.error('Sample message2');
37+
logger.error('Sample message3');
38+
logger.info('Sample message4');
39+
expect(logger.get()).toEqual([
40+
{
41+
type: 'info',
42+
timestamp: fakeTimeEpox,
43+
message: 'Sample message1',
44+
},
45+
{
46+
type: 'error',
47+
timestamp: fakeTimeEpox,
48+
message: 'Sample message2',
49+
},
50+
{
51+
type: 'error',
52+
timestamp: fakeTimeEpox,
53+
message: 'Sample message3',
54+
},
55+
{
56+
type: 'info',
57+
timestamp: fakeTimeEpox,
58+
message: 'Sample message4',
59+
},
60+
]);
61+
});
62+
});
63+
64+
describe('get', () => {
65+
it('should get "all" logs (by default or explicit)', () => {
66+
expect(logger.get()).toEqual([]);
67+
logger.info('');
68+
logger.error('');
69+
logger.info('');
70+
71+
const expected = ['info', 'error', 'info'];
72+
73+
expect(logger.get().map((entry) => entry.type)).toEqual(expected);
74+
expect(logger.get('all').map((entry) => entry.type)).toEqual(expected);
75+
});
76+
77+
it('should get "info" logs', () => {
78+
logger.info('');
79+
logger.error('');
80+
logger.info('');
81+
82+
const expected = ['info', 'info'];
83+
84+
expect(logger.get('info').map((entry) => entry.type)).toEqual(expected);
85+
});
86+
87+
it('should get "error" logs', () => {
88+
logger.info('');
89+
logger.error('');
90+
logger.info('');
91+
92+
const expected = ['error'];
93+
94+
expect(logger.get('error').map((entry) => entry.type)).toEqual(expected);
95+
});
96+
});
97+
98+
describe('getSuggestLogfilePath', () => {
99+
it('the path should includes the os tmp dir', () => {
100+
const path = logger.getSuggestLogfilePath();
101+
expect(path.includes('/tmpDir')).toBeTruthy();
102+
});
103+
104+
it('should return uniq paths', () => {
105+
const changeDate = (date: string) =>
106+
jest.useFakeTimers().setSystemTime(new Date(date));
107+
changeDate('2026-01-01 10:20:16:36');
108+
109+
const path1 = logger.getSuggestLogfilePath();
110+
changeDate('2026-01-01 10:20:16:37');
111+
112+
const path2 = logger.getSuggestLogfilePath();
113+
changeDate('2026-01-01 10:20:16:38');
114+
115+
const path3 = logger.getSuggestLogfilePath();
116+
117+
const isSamePath = path1 === path2 || path1 === path3 || path2 === path3;
118+
expect(isSamePath).not.toBeTruthy();
119+
});
120+
});
121+
122+
describe('saveToFile', () => {
123+
it('shoul write the content of the log to a given file', () => {
124+
const path = '/tmp/npkill-log.log';
125+
logger.info('hello');
126+
logger.error('bye');
127+
logger.info('world');
128+
const expected =
129+
'[1767225600000](info) hello\n' +
130+
'[1767225600000](error) bye\n' +
131+
'[1767225600000](info) world\n';
132+
133+
logger.saveToFile(path);
134+
expect(writeFileSyncMock).toBeCalledWith(path, expected);
135+
});
136+
});
137+
});

0 commit comments

Comments
 (0)