Skip to content

Commit 77ff89d

Browse files
Fix tests
1 parent 1576d0c commit 77ff89d

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

packages/snaps-utils/coverage.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"branches": 99.53,
2+
"branches": 99.76,
33
"functions": 99.02,
4-
"lines": 98.52,
5-
"statements": 97.12
4+
"lines": 98.66,
5+
"statements": 97.25
66
}

packages/snaps-utils/src/fs.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,26 @@ describe('useFileSystemCache', () => {
334334
value: 'foo',
335335
});
336336
});
337+
338+
it('skips persisting undefined', async () => {
339+
const fn = useFileSystemCache('foo', 5000, async () => {
340+
return undefined;
341+
});
342+
343+
const spy = jest.spyOn(fs, 'writeFile');
344+
expect(await fn()).toBeUndefined();
345+
346+
expect(spy).toHaveBeenCalledTimes(0);
347+
});
348+
349+
it('skips persisting null', async () => {
350+
const fn = useFileSystemCache('foo', 5000, async () => {
351+
return null;
352+
});
353+
354+
const spy = jest.spyOn(fs, 'writeFile');
355+
expect(await fn()).toBeNull();
356+
357+
expect(spy).toHaveBeenCalledTimes(0);
358+
});
337359
});

packages/snaps-utils/src/fs.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ export function useFileSystemCache<Type = unknown>(
216216

217217
const value = await fn();
218218

219+
// Null or undefined is not persisted.
220+
if (value === null || value === undefined) {
221+
return value;
222+
}
223+
219224
try {
220225
await fs.mkdir(pathUtils.dirname(filePath), { recursive: true });
221226

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { SemVerVersion } from '@metamask/utils';
22
import assert from 'assert';
3+
import { promises as fs } from 'fs';
34
import fetchMock from 'jest-fetch-mock';
5+
import { join } from 'path';
46

57
import { productionPlatformVersion } from './production-platform-version';
68
import { getMockSnapFiles, getSnapManifest } from '../../test-utils';
@@ -18,11 +20,19 @@ const MOCK_PACKAGE_JSON = JSON.stringify({
1820
},
1921
});
2022

23+
const CACHE_PATH = join(process.cwd(), 'node_modules/.cache');
24+
2125
describe('productionPlatformVersion', () => {
2226
beforeAll(() => {
2327
fetchMock.enableMocks();
2428
});
2529

30+
beforeEach(async () => {
31+
fetchMock.resetMocks();
32+
33+
await fs.rm(CACHE_PATH, { recursive: true, force: true });
34+
});
35+
2636
afterAll(() => {
2737
fetchMock.disableMocks();
2838
});
@@ -98,7 +108,7 @@ describe('productionPlatformVersion', () => {
98108
});
99109

100110
it('does nothing if the request to check the production version fails', async () => {
101-
fetchMock.mockResponse('', { status: 404 });
111+
fetchMock.mockResponse(async () => ({ status: 404, body: 'Not found' }));
102112

103113
const report = jest.fn();
104114
assert(productionPlatformVersion.semanticCheck);

0 commit comments

Comments
 (0)