Skip to content

Commit

Permalink
fix: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptodev-2s committed Jan 10, 2025
1 parent dcd4406 commit 0b4a8b9
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions scripts/create-package/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ describe('create-package/utils', () => {
nodeVersions: '>=18.0.0',
};

(fs.promises.stat as jest.Mock).mockResolvedValueOnce({
isDirectory: () => false,
});
const mockError = new Error('Not found') as NodeJS.ErrnoException;
mockError.code = 'ENOENT';

jest.spyOn(fs.promises, 'stat').mockRejectedValue(mockError);

(fsUtils.readAllFiles as jest.Mock).mockResolvedValueOnce({
'src/index.ts': 'export default 42;',
Expand Down Expand Up @@ -181,5 +182,34 @@ describe('create-package/utils', () => {
expect(fs.promises.mkdir).not.toHaveBeenCalled();
expect(fs.promises.writeFile).not.toHaveBeenCalled();
});

it('throws if fs.stat fails with an error other than ENOENT', async () => {
const mockError = new Error('Permission denied') as NodeJS.ErrnoException;
mockError.code = 'EACCES';

jest.spyOn(fs.promises, 'stat').mockRejectedValue(mockError);

const packageData: PackageData = {
name: '@metamask/foo',
description: 'A foo package.',
directoryName: 'foo',
nodeVersions: '20.0.0',
currentYear: '2023',
};

const monorepoFileData = {
tsConfig: {
references: [{ path: './packages/bar' }],
},
tsConfigBuild: {
references: [{ path: './packages/bar' }],
},
nodeVersions: '20.0.0',
};

await expect(
finalizeAndWriteData(packageData, monorepoFileData),
).rejects.toThrow('Permission denied');
});
});
});

0 comments on commit 0b4a8b9

Please sign in to comment.