Skip to content

Commit 7d8ee9c

Browse files
committed
Keep the Date spy and only scope the colon check to the filename
The earlier switch to jest fake timers froze time for the rest of the suite and broke the test on every platform. The Date constructor spy already pins the timestamp correctly, as the original Windows failure showed the right pinned value. The only real problem was that the colon check looked at the full path, which on Windows starts with a drive letter like D: that legitimately contains a colon. Restore the spy and keep the assertion scoped to the basename.
1 parent 8c2c912 commit 7d8ee9c

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

libraries/rush-lib/src/api/test/ChangeFile.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ describe(ChangeFile.name, () => {
2020
.mockReturnValue({ branch: 'my-branch' } as any);
2121

2222
// Pin the clock to 2017-05-01 20:20:30 UTC so the timestamp is deterministic.
23-
// Use Jest fake timers so new Date() is intercepted at the engine level, which is
24-
// more reliable than spying on the Date constructor (the spy approach is fragile on
25-
// Windows with Node.js v24 because the V8 Date fast-path can bypass the spy).
26-
jest.useFakeTimers().setSystemTime(new Date('2017-05-01T20:20:30.000Z'));
23+
const fixedDate: Date = new Date('2017-05-01T20:20:30.000Z');
24+
const dateSpy: jest.SpyInstance = jest
25+
.spyOn(global, 'Date')
26+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
27+
.mockImplementation(() => fixedDate as any);
2728

2829
try {
2930
const changeFile: ChangeFile = new ChangeFile(
@@ -46,7 +47,7 @@ describe(ChangeFile.name, () => {
4647
// includes a drive letter (e.g. "D:\...") which legitimately contains a colon.
4748
expect(filename).not.toContain(':');
4849
} finally {
49-
jest.useRealTimers();
50+
dateSpy.mockRestore();
5051
getGitInfoSpy.mockRestore();
5152
}
5253
});

0 commit comments

Comments
 (0)