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 0b4a8b9 commit 1f539bb
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions scripts/create-package/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,92 @@ describe('create-package/utils', () => {
});
});

it('should write the expected files if the directory does not exist', async () => {
const packageData: PackageData = {
name: '@metamask/foo',
description: 'A foo package.',
directoryName: 'foo',
nodeVersions: '>=18.0.0',
currentYear: '2023',
};

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

const mockError = new Error('Not found') as NodeJS.ErrnoException;
mockError.code = 'ENOENT';

(fs.promises.stat as jest.Mock).mockResolvedValueOnce({
isDirectory: () => false,
});

(fsUtils.readAllFiles as jest.Mock).mockResolvedValueOnce({
'src/index.ts': 'export default 42;',
'src/index.test.ts': 'export default 42;',
'mock1.file':
'CURRENT_YEAR NODE_VERSIONS PACKAGE_NAME PACKAGE_DESCRIPTION PACKAGE_DIRECTORY_NAME',
'mock2.file': 'CURRENT_YEAR NODE_VERSIONS PACKAGE_NAME',
'mock3.file': 'PACKAGE_DESCRIPTION PACKAGE_DIRECTORY_NAME',
});

(prettier.format as jest.Mock).mockImplementation((input) => input);

await finalizeAndWriteData(packageData, monorepoFileData);

// processTemplateFiles and writeFiles
expect(fsUtils.readAllFiles).toHaveBeenCalledTimes(1);
expect(fsUtils.readAllFiles).toHaveBeenCalledWith(
expect.stringMatching(/\/package-template$/u),
);

expect(fsUtils.writeFiles).toHaveBeenCalledTimes(1);
expect(fsUtils.writeFiles).toHaveBeenCalledWith(
expect.stringMatching(/packages\/foo$/u),
{
'src/index.ts': 'export default 42;',
'src/index.test.ts': 'export default 42;',
'mock1.file': '2023 >=18.0.0 @metamask/foo A foo package. foo',
'mock2.file': '2023 >=18.0.0 @metamask/foo',
'mock3.file': 'A foo package. foo',
},
);

// Writing monorepo files
expect(fs.promises.writeFile).toHaveBeenCalledTimes(2);
expect(prettier.format).toHaveBeenCalledTimes(2);
expect(fs.promises.writeFile).toHaveBeenCalledWith(
expect.stringMatching(/tsconfig\.json$/u),
JSON.stringify({
references: [{ path: './packages/bar' }, { path: './packages/foo' }],
}),
);
expect(fs.promises.writeFile).toHaveBeenCalledWith(
expect.stringMatching(/tsconfig\.build\.json$/u),
JSON.stringify({
references: [
{ path: './packages/bar' },
{ path: './packages/foo/tsconfig.build.json' },
],
}),
);

// Postprocessing
expect(execa).toHaveBeenCalledTimes(2);
expect(execa).toHaveBeenCalledWith('yarn', ['install'], {
cwd: expect.any(String),
});
expect(execa).toHaveBeenCalledWith('yarn', ['update-readme-content'], {
cwd: expect.any(String),
});
});

it('throws if the package directory already exists', async () => {
const packageData: PackageData = {
name: '@metamask/foo',
Expand Down

0 comments on commit 1f539bb

Please sign in to comment.