Skip to content

Commit 58b3534

Browse files
authored
chore(cli): ensure version tests don't make network calls (#91)
Previously some of these test cases made an actual call to npm. This is potentially slow and flaky. Ensure all current and future test cases mock expected results or fail. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 70e4081 commit 58b3534

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/aws-cdk/test/cli/version.test.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function tmpfile(): string {
1818
}
1919

2020
beforeEach(() => {
21+
jest.spyOn(npm, 'getLatestVersionFromNpm').mockRejectedValue(new Error('expected version cache to be used, but call to npm was attempted'));
2122
process.chdir(os.tmpdir()); // Need a chdir because in the workspace 'npm view' will take a long time
2223
});
2324

@@ -53,6 +54,7 @@ test('Skip version check if cache has not expired', async () => {
5354

5455
test('Return later version when exists & skip recent re-check', async () => {
5556
const cache = new VersionCheckTTL(tmpfile(), 100);
57+
jest.spyOn(npm, 'getLatestVersionFromNpm').mockResolvedValueOnce('1.1.0');
5658
const result = await latestVersionIfHigher('0.0.0', cache);
5759
expect(result).not.toBeNull();
5860
expect((result as string).length).toBeGreaterThan(0);
@@ -63,6 +65,7 @@ test('Return later version when exists & skip recent re-check', async () => {
6365

6466
test('Return null if version is higher than npm', async () => {
6567
const cache = new VersionCheckTTL(tmpfile(), 100);
68+
jest.spyOn(npm, 'getLatestVersionFromNpm').mockResolvedValueOnce('1.1.0');
6669
const result = await latestVersionIfHigher('100.100.100', cache);
6770
expect(result).toBeNull();
6871
});
@@ -104,7 +107,7 @@ describe('version message', () => {
104107
test('Prints a message when a new version is available', async () => {
105108
// Given the current version is 1.0.0 and the latest version is 1.1.0
106109
const currentVersion = '1.0.0';
107-
jest.spyOn(npm, 'getLatestVersionFromNpm').mockResolvedValue('1.1.0');
110+
jest.spyOn(npm, 'getLatestVersionFromNpm').mockResolvedValueOnce('1.1.0');
108111
const printSpy = jest.spyOn(logging, 'info');
109112

110113
// When displayVersionMessage is called
@@ -117,7 +120,7 @@ describe('version message', () => {
117120
test('Includes major upgrade documentation when available', async() => {
118121
// Given the current version is 1.0.0 and the latest version is 2.0.0
119122
const currentVersion = '1.0.0';
120-
jest.spyOn(npm, 'getLatestVersionFromNpm').mockResolvedValue('2.0.0');
123+
jest.spyOn(npm, 'getLatestVersionFromNpm').mockResolvedValueOnce('2.0.0');
121124
const printSpy = jest.spyOn(logging, 'info');
122125

123126
// When displayVersionMessage is called
@@ -130,7 +133,7 @@ describe('version message', () => {
130133
test('Does not include major upgrade documentation when unavailable', async() => {
131134
// Given current version is 99.0.0 and the latest version is 100.0.0
132135
const currentVersion = '99.0.0';
133-
jest.spyOn(npm, 'getLatestVersionFromNpm').mockResolvedValue('100.0.0');
136+
jest.spyOn(npm, 'getLatestVersionFromNpm').mockResolvedValueOnce('100.0.0');
134137
const printSpy = jest.spyOn(logging, 'info');
135138

136139
// When displayVersionMessage is called

0 commit comments

Comments
 (0)