Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/webpack-cli/lib/utils/__tests__/resolve-command.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
jest.setMock('../prompt-installation', jest.fn());

const resolveCommand = require('../resolve-command');
const promptInstallation = require('../prompt-installation');

describe('resolve-command util', () => {
const processExitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {});
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

beforeEach(() => {
processExitSpy.mockClear();
consoleErrorSpy.mockClear();
});

it('should not throw error', async () => {
promptInstallation.mockImplementation(() => {});

await expect(resolveCommand('info')).resolves.not.toThrow();
expect(processExitSpy.mock.calls.length).toBe(0);
expect(consoleErrorSpy.mock.calls.length).toBe(0);
});

it('should throw error and exit with invalid command', async () => {
promptInstallation.mockImplementation(() => {
throw new Error();
});

await resolveCommand('invalid');
expect(processExitSpy).toBeCalledWith(2);
expect(consoleErrorSpy.mock.calls.length).toBe(1);
});
});
1 change: 1 addition & 0 deletions packages/webpack-cli/lib/utils/resolve-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const run = async (name, ...args) => {
});
} catch (err) {
logger.error(`Action Interrupted, use ${cyan('webpack-cli help')} to see possible commands.`);
process.exit(2);
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/webpack-cli/lib/utils/run-command.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const execa = require('execa');
const logger = require('./logger');

async function runCommand(command, args = []) {
try {
Expand All @@ -7,7 +8,8 @@ async function runCommand(command, args = []) {
shell: true,
});
} catch (e) {
throw new Error(e);
logger.error(e);
process.exit(2);
}
}

Expand Down