Skip to content

Commit a14be6f

Browse files
committed
Clean up jest mocking in the new test.
1 parent 89d2e7a commit a14be6f

1 file changed

Lines changed: 18 additions & 33 deletions

File tree

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

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import * as path from 'node:path';
4+
import type { GitRepoInfo } from 'git-repo-info';
55

66
import { ChangeFile } from '../ChangeFile';
77
import { RushConfiguration } from '../RushConfiguration';
@@ -14,42 +14,27 @@ describe(ChangeFile.name, () => {
1414
const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile(rushFilename);
1515

1616
// Pin the branch name so the generated filename is deterministic.
17-
const getGitInfoSpy: jest.SpyInstance = jest
18-
.spyOn(Git.prototype, 'getGitInfo')
19-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20-
.mockReturnValue({ branch: 'my-branch' } as any);
17+
jest.spyOn(Git.prototype, 'getGitInfo').mockReturnValue({ branch: 'my-branch' } as Readonly<GitRepoInfo>);
2118

2219
// Pin the clock to 2017-05-01 20:20:30 UTC so the timestamp is deterministic.
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);
28-
29-
try {
30-
const changeFile: ChangeFile = new ChangeFile(
31-
{
32-
packageName: 'a',
33-
changes: [],
34-
email: 'fake@microsoft.com'
35-
},
36-
rushConfiguration
37-
);
20+
jest.useFakeTimers({
21+
now: new Date('2017-05-01T20:20:30.000Z').getTime()
22+
});
3823

39-
const generatedPath: string = changeFile.generatePath();
40-
const filename: string = path.basename(generatedPath);
24+
const changeFile: ChangeFile = new ChangeFile(
25+
{
26+
packageName: 'a',
27+
changes: [],
28+
email: 'fake@microsoft.com'
29+
},
30+
rushConfiguration
31+
);
4132

42-
// The seconds must be present and the filename must be fully dash-separated
43-
// (no leftover colons from the time portion).
44-
// Check toContain on the forward-slash-normalised path so it works on Windows too.
45-
expect(generatedPath.replace(/\\/g, '/')).toContain('my-branch_2017-05-01-20-20-30.json');
46-
// Only check the filename for colons, not the full path: on Windows the path
47-
// includes a drive letter (e.g. "D:\...") which legitimately contains a colon.
48-
expect(filename).not.toContain(':');
49-
} finally {
50-
dateSpy.mockRestore();
51-
getGitInfoSpy.mockRestore();
52-
}
33+
const generatedPath: string = changeFile.generatePath();
34+
// The seconds must be present and the filename must be fully dash-separated
35+
// (no leftover colons from the time portion).
36+
// Check toContain on the forward-slash-normalised path so it works on Windows too.
37+
expect(generatedPath.replace(/\\/g, '/').endsWith('my-branch_2017-05-01-20-20-30.json')).toBe(true);
5338
});
5439

5540
it('can add a change', () => {

0 commit comments

Comments
 (0)