Skip to content

Commit 7254a69

Browse files
committed
test(logger): fix and add test for logfile rotation
1 parent b9dd681 commit 7254a69

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

__tests__/logger.service.test.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { jest } from '@jest/globals';
22

33
const writeFileSyncMock = jest.fn();
4+
const renameFileSyncMock = jest.fn();
5+
const existsSyncMock = jest.fn();
46
jest.unstable_mockModule('fs', () => {
57
return {
68
writeFileSync: writeFileSyncMock,
9+
existsSync: existsSyncMock,
10+
renameSync: renameFileSyncMock,
711
default: jest.fn(),
812
};
913
});
@@ -100,22 +104,22 @@ describe('LoggerService', () => {
100104
const path = logger.getSuggestLogfilePath();
101105
expect(path.includes('/tmpDir')).toBeTruthy();
102106
});
107+
});
103108

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();
109+
describe('LogFile rotation', () => {
110+
it('should not rotate file if not exist', () => {
111+
existsSyncMock.mockReturnValue(false);
112+
const path = logger.getSuggestLogfilePath();
113+
logger.saveToFile(path);
114+
expect(renameFileSyncMock).not.toBeCalled();
115+
});
116116

117-
const isSamePath = path1 === path2 || path1 === path3 || path2 === path3;
118-
expect(isSamePath).not.toBeTruthy();
117+
it('should rotate file if exist', () => {
118+
existsSyncMock.mockReturnValue(true);
119+
const path = logger.getSuggestLogfilePath();
120+
logger.saveToFile(path);
121+
const expectedOldPath = path.replace('latest', 'old');
122+
expect(renameFileSyncMock).toBeCalledWith(path, expectedOldPath);
119123
});
120124
});
121125

0 commit comments

Comments
 (0)