Skip to content

Commit 0b4a8b9

Browse files
committed
fix: add unit tests
1 parent dcd4406 commit 0b4a8b9

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

scripts/create-package/utils.test.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ describe('create-package/utils', () => {
8787
nodeVersions: '>=18.0.0',
8888
};
8989

90-
(fs.promises.stat as jest.Mock).mockResolvedValueOnce({
91-
isDirectory: () => false,
92-
});
90+
const mockError = new Error('Not found') as NodeJS.ErrnoException;
91+
mockError.code = 'ENOENT';
92+
93+
jest.spyOn(fs.promises, 'stat').mockRejectedValue(mockError);
9394

9495
(fsUtils.readAllFiles as jest.Mock).mockResolvedValueOnce({
9596
'src/index.ts': 'export default 42;',
@@ -181,5 +182,34 @@ describe('create-package/utils', () => {
181182
expect(fs.promises.mkdir).not.toHaveBeenCalled();
182183
expect(fs.promises.writeFile).not.toHaveBeenCalled();
183184
});
185+
186+
it('throws if fs.stat fails with an error other than ENOENT', async () => {
187+
const mockError = new Error('Permission denied') as NodeJS.ErrnoException;
188+
mockError.code = 'EACCES';
189+
190+
jest.spyOn(fs.promises, 'stat').mockRejectedValue(mockError);
191+
192+
const packageData: PackageData = {
193+
name: '@metamask/foo',
194+
description: 'A foo package.',
195+
directoryName: 'foo',
196+
nodeVersions: '20.0.0',
197+
currentYear: '2023',
198+
};
199+
200+
const monorepoFileData = {
201+
tsConfig: {
202+
references: [{ path: './packages/bar' }],
203+
},
204+
tsConfigBuild: {
205+
references: [{ path: './packages/bar' }],
206+
},
207+
nodeVersions: '20.0.0',
208+
};
209+
210+
await expect(
211+
finalizeAndWriteData(packageData, monorepoFileData),
212+
).rejects.toThrow('Permission denied');
213+
});
184214
});
185215
});

0 commit comments

Comments
 (0)