Skip to content

Commit

Permalink
bump coverage in initHandler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeRx committed Nov 3, 2022
1 parent f3efb2d commit 00dccca
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 11 deletions.
20 changes: 10 additions & 10 deletions packages/cli/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import yargs from 'yargs';
import { cli } from './cli';
import commands from './cmds';

// Removes positional arguments from commands. eg. 'init [directory]' -> 'init'
const sanitizeCommand = (command: string) =>
command.replace(/(\[.*?\])/u, '').trim();

const commandMap = (commands as unknown as yargs.CommandModule[]).reduce(
(map, commandModule) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
map[commandModule.command![0]] = commandModule;
map[sanitizeCommand(commandModule.command![0])] = commandModule;
return map;
},
{} as Record<string, yargs.CommandModule>,
Expand All @@ -15,10 +19,6 @@ const getMockArgv = (...args: string[]) => {
return ['/mock/path', '/mock/entry/path', ...args];
};

// Removes positional arguments from commands. eg. 'init [directory]' -> 'init'
const sanitizeCommand = (command: string) =>
command.replace(/(\[.*?\])/u, '').trim();

// The ".+" is because the CLI name (specified to yargs as "$0") is
// populated programmatically based on the name of entry point file.
// In Jest, that's sometimes "childProcess.js", sometimes other things.
Expand Down Expand Up @@ -140,15 +140,15 @@ describe('cli', () => {
});

it('handles an error thrown by a locally defined command handler', () => {
const mockBuildHandler = jest.fn().mockImplementation(() => {
throw new Error('build failed');
const mockInitHandler = jest.fn().mockImplementation(() => {
throw new Error('init failed');
});

expect(() =>
cli(getMockArgv('build'), [
{ ...commandMap.build, handler: mockBuildHandler },
cli(getMockArgv('init'), [
{ ...commandMap.init, handler: mockInitHandler },
]),
).toThrow('build failed');
).toThrow('init failed');
});
});
});
104 changes: 103 additions & 1 deletion packages/cli/src/cmds/init/initHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getPackageJson,
getSnapManifest,
} from '@metamask/snap-utils/test-utils';
import { YargsArgs } from '../../types/yargs';
import { initHandler } from './initHandler';
import * as initUtils from './initUtils';

Expand All @@ -22,7 +23,63 @@ describe('initialize', () => {
global.snaps = {};
});

it('successfully initializes a Snap project', async () => {
it('successfully initializes a Snap project in the current working directory', async () => {
const satisfiesVersionRangeMock = jest
.spyOn(snapUtils, 'satisfiesVersionRange')
.mockImplementation(() => true);
const isGitInstalledMock = jest
.spyOn(initUtils, 'isGitInstalled')
.mockImplementation(() => true);
const prepareWorkingDirectoryMock = jest
.spyOn(initUtils, 'prepareWorkingDirectory')
.mockImplementation();
const consoleLogMock = jest.spyOn(console, 'log').mockImplementation();
const cloneTemplateMock = jest
.spyOn(initUtils, 'cloneTemplate')
.mockImplementation();
const rmMock = jest.spyOn(fs, 'rm').mockImplementation();
const yarnInstallMock = jest
.spyOn(initUtils, 'yarnInstall')
.mockImplementation();
const isInGitRepositoryMock = jest
.spyOn(initUtils, 'isInGitRepository')
.mockImplementation(() => false);

const gitInitMock = jest.spyOn(initUtils, 'gitInit').mockImplementation();

const readJsonFileMock = jest
.spyOn(snapUtils, 'readJsonFile')
.mockImplementationOnce(async () => getSnapManifest());

jest
.spyOn(snapUtils, 'readJsonFile')
.mockImplementationOnce(async () => getPackageJson());

const expected = {
dist: 'dist',
outfileName: 'bundle.js',
src: 'src/index.js',
snapLocation: pathUtils.join(process.cwd(), initUtils.SNAP_LOCATION),
};

expect(await initHandler({} as YargsArgs)).toStrictEqual({
...expected,
});

expect(satisfiesVersionRangeMock).toHaveBeenCalledTimes(1);
expect(isGitInstalledMock).toHaveBeenCalledTimes(1);
expect(prepareWorkingDirectoryMock).toHaveBeenCalledTimes(1);
expect(prepareWorkingDirectoryMock).toHaveBeenCalledWith(process.cwd());
expect(consoleLogMock).toHaveBeenCalledTimes(4);
expect(cloneTemplateMock).toHaveBeenCalledTimes(1);
expect(rmMock).toHaveBeenCalledTimes(1);
expect(yarnInstallMock).toHaveBeenCalledTimes(1);
expect(isInGitRepositoryMock).toHaveBeenCalledTimes(1);
expect(gitInitMock).toHaveBeenCalledTimes(1);
expect(readJsonFileMock).toHaveBeenCalledTimes(2);
});

it('successfully initializes a Snap project in a given directory', async () => {
const satisfiesVersionRangeMock = jest
.spyOn(snapUtils, 'satisfiesVersionRange')
.mockImplementation(() => true);
Expand Down Expand Up @@ -72,6 +129,9 @@ describe('initialize', () => {
expect(satisfiesVersionRangeMock).toHaveBeenCalledTimes(1);
expect(isGitInstalledMock).toHaveBeenCalledTimes(1);
expect(prepareWorkingDirectoryMock).toHaveBeenCalledTimes(1);
expect(prepareWorkingDirectoryMock).toHaveBeenCalledWith(
pathUtils.join(process.cwd(), getMockArgv().directory),
);
expect(consoleLogMock).toHaveBeenCalledTimes(4);
expect(cloneTemplateMock).toHaveBeenCalledTimes(1);
expect(rmMock).toHaveBeenCalledTimes(1);
Expand All @@ -81,6 +141,48 @@ describe('initialize', () => {
expect(readJsonFileMock).toHaveBeenCalledTimes(2);
});

it("defaults to 'src/index.js' if there is no main entry in package.json", async () => {
jest
.spyOn(snapUtils, 'satisfiesVersionRange')
.mockImplementation(() => true);

jest.spyOn(initUtils, 'isGitInstalled').mockImplementation(() => true);

jest.spyOn(initUtils, 'prepareWorkingDirectory').mockImplementation();
jest.spyOn(console, 'log').mockImplementation();
jest.spyOn(initUtils, 'cloneTemplate').mockImplementation();
jest.spyOn(fs, 'rm').mockImplementation();
jest.spyOn(initUtils, 'yarnInstall').mockImplementation();

jest
.spyOn(initUtils, 'isInGitRepository')
.mockImplementation(() => false);
jest.spyOn(initUtils, 'gitInit').mockImplementation();

jest
.spyOn(snapUtils, 'readJsonFile')
.mockImplementationOnce(async () => getSnapManifest());

jest
.spyOn(snapUtils, 'readJsonFile')
.mockImplementationOnce(async () => ({ main: undefined }));

const expected = {
...getMockArgv(),
dist: 'dist',
outfileName: 'bundle.js',
src: 'src/index.js',
snapLocation: pathUtils.join(
process.cwd(),
`foo/${initUtils.SNAP_LOCATION}`,
),
};

expect(await initHandler({ ...getMockArgv() })).toStrictEqual({
...expected,
});
});

it("doesn't init if it's already in a git repo", async () => {
jest
.spyOn(snapUtils, 'satisfiesVersionRange')
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/cmds/init/initHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function initHandler(argv: YargsArgs) {
process.version,
SATISFIED_VERSION,
);

if (!isVersionSupported) {
logError(
`Init Error: You are using an outdated version of Node (${process.version}). Please update to Node ${SATISFIED_VERSION}.`,
Expand Down

0 comments on commit 00dccca

Please sign in to comment.