Skip to content

Commit c9b61aa

Browse files
Add test coverage
1 parent 9be26ef commit c9b61aa

File tree

5 files changed

+123
-4
lines changed

5 files changed

+123
-4
lines changed

packages/snaps-utils/jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ module.exports = deepmerge(baseConfig, {
1919
// https://github.com/facebook/jest/issues/5274
2020
'./src/eval-worker.ts',
2121
],
22+
23+
// This is required for `jest-fetch-mock` to work.
24+
resetMocks: false,
2225
});

packages/snaps-utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
"istanbul-lib-report": "^3.0.0",
126126
"istanbul-reports": "^3.1.5",
127127
"jest": "^29.0.2",
128+
"jest-fetch-mock": "^3.0.3",
128129
"jest-silent-reporter": "^0.6.0",
129130
"memfs": "^3.4.13",
130131
"prettier": "^3.3.3",
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import type { SemVerVersion } from '@metamask/utils';
2+
import assert from 'assert';
3+
import fetchMock from 'jest-fetch-mock';
4+
5+
import { productionPlatformVersion } from './production-platform-version';
6+
import { getMockSnapFiles, getSnapManifest } from '../../test-utils';
7+
8+
const MOCK_GITHUB_RESPONSE = JSON.stringify({
9+
// eslint-disable-next-line @typescript-eslint/naming-convention
10+
target_commitish: '5fceb7ed2ef18a3984786db1161a76ca5c8e15b9',
11+
});
12+
13+
const MOCK_PACKAGE_JSON = JSON.stringify({
14+
dependencies: {
15+
'@metamask/snaps-sdk': '6.1.0',
16+
},
17+
});
18+
19+
describe('productionPlatformVersion', () => {
20+
beforeAll(() => {
21+
fetchMock.enableMocks();
22+
});
23+
24+
afterAll(() => {
25+
fetchMock.disableMocks();
26+
});
27+
28+
it('reports if the version is greater than the production version', async () => {
29+
fetchMock.mockResponses(MOCK_GITHUB_RESPONSE, MOCK_PACKAGE_JSON);
30+
31+
const report = jest.fn();
32+
assert(productionPlatformVersion.semanticCheck);
33+
34+
await productionPlatformVersion.semanticCheck(
35+
getMockSnapFiles({
36+
manifest: getSnapManifest({
37+
platformVersion: '6.5.0' as SemVerVersion,
38+
}),
39+
}),
40+
{ report },
41+
);
42+
43+
expect(report).toHaveBeenCalledTimes(1);
44+
});
45+
46+
it('does nothing if the version is less than the production version', async () => {
47+
fetchMock.mockResponses(MOCK_GITHUB_RESPONSE, MOCK_PACKAGE_JSON);
48+
49+
const report = jest.fn();
50+
assert(productionPlatformVersion.semanticCheck);
51+
52+
await productionPlatformVersion.semanticCheck(
53+
getMockSnapFiles({
54+
manifest: getSnapManifest({
55+
platformVersion: '6.0.0' as SemVerVersion,
56+
}),
57+
}),
58+
{ report },
59+
);
60+
61+
expect(report).toHaveBeenCalledTimes(0);
62+
});
63+
64+
it('does nothing if the version is equal to the production version', async () => {
65+
fetchMock.mockResponses(MOCK_GITHUB_RESPONSE, MOCK_PACKAGE_JSON);
66+
67+
const report = jest.fn();
68+
assert(productionPlatformVersion.semanticCheck);
69+
70+
await productionPlatformVersion.semanticCheck(
71+
getMockSnapFiles({
72+
manifest: getSnapManifest({
73+
platformVersion: '6.1.0' as SemVerVersion,
74+
}),
75+
}),
76+
{ report },
77+
);
78+
79+
expect(report).toHaveBeenCalledTimes(0);
80+
});
81+
82+
it('does nothing if the version is not set', async () => {
83+
const report = jest.fn();
84+
assert(productionPlatformVersion.semanticCheck);
85+
86+
const rawManifest = getSnapManifest();
87+
delete rawManifest.platformVersion;
88+
89+
const files = getMockSnapFiles({
90+
manifest: rawManifest,
91+
});
92+
93+
await productionPlatformVersion.semanticCheck(files, { report });
94+
95+
expect(report).toHaveBeenCalledTimes(0);
96+
});
97+
98+
it('does nothing if the request to check the production version fails', async () => {
99+
fetchMock.mockResponse('', { status: 404 });
100+
101+
const report = jest.fn();
102+
assert(productionPlatformVersion.semanticCheck);
103+
104+
const files = getMockSnapFiles();
105+
106+
await productionPlatformVersion.semanticCheck(files, { report });
107+
108+
expect(report).toHaveBeenCalledTimes(0);
109+
});
110+
});

packages/snaps-utils/src/manifest/validators/production-platform-version.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ async function determineProductionVersion() {
1313
// TODO: Cache this check.
1414
const latestRelease = await fetch(
1515
'https://api.github.com/repos/metamask/metamask-extension/releases/latest',
16-
).then(async (response) => response.json());
16+
);
1717

18-
const latestReleaseCommit = latestRelease.target_commitish;
18+
const latestReleaseJson = await latestRelease.json();
1919

20-
const packageJson = await fetch(
20+
const latestReleaseCommit = latestReleaseJson.target_commitish;
21+
22+
const packageJsonResponse = await fetch(
2123
`https://raw.githubusercontent.com/MetaMask/metamask-extension/${latestReleaseCommit}/package.json`,
22-
).then(async (response) => response.json());
24+
);
25+
26+
const packageJson = await packageJsonResponse.json();
2327

2428
const versionRange = packageJson.dependencies['@metamask/snaps-sdk'];
2529

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4564,6 +4564,7 @@ __metadata:
45644564
istanbul-lib-report: "npm:^3.0.0"
45654565
istanbul-reports: "npm:^3.1.5"
45664566
jest: "npm:^29.0.2"
4567+
jest-fetch-mock: "npm:^3.0.3"
45674568
jest-silent-reporter: "npm:^0.6.0"
45684569
luxon: "npm:^3.5.0"
45694570
marked: "npm:^12.0.1"

0 commit comments

Comments
 (0)