diff --git a/packages/package-utils/__tests__/index.test.ts b/packages/package-utils/__tests__/index.test.ts index bb67a82eae2..1364653d235 100644 --- a/packages/package-utils/__tests__/index.test.ts +++ b/packages/package-utils/__tests__/index.test.ts @@ -1,11 +1,19 @@ 'use strict'; -import { packageExists } from '../src'; +jest.mock('@webpack-cli/package-utils') + +import { packageExists, promptInstallation } from '@webpack-cli/package-utils'; +import ExternalCommand from '../../webpack-cli/lib/commands/ExternalCommand'; describe('@webpack-cli/package-utils', () => { it('should check existence of package', () => { + (packageExists as any).mockImplementation(() => true); const exists = packageExists('@webpack-cli/info'); - expect(exists).toBeTruthy(); }); + + it('should not throw if the user interrupts', async () => { + (promptInstallation as any).mockImplementation(() => { throw new Error() }); + await expect(ExternalCommand.run('info')).resolves.not.toThrow(); + }); }); diff --git a/packages/webpack-cli/lib/commands/ExternalCommand.js b/packages/webpack-cli/lib/commands/ExternalCommand.js index 6eeb617279c..ad89839431f 100644 --- a/packages/webpack-cli/lib/commands/ExternalCommand.js +++ b/packages/webpack-cli/lib/commands/ExternalCommand.js @@ -27,9 +27,13 @@ class ExternalCommand { const scopeName = packagePrefix + '/' + name; let pkgLoc = packageExists(scopeName); if (!pkgLoc) { - pkgLoc = await promptInstallation(`${scopeName}`, () => { + try { + pkgLoc = await promptInstallation(`${scopeName}`, () => { logger.error(`The command moved into a separate package: ${chalk.keyword('orange')(scopeName)}\n`); - }); + }); + } catch (err) { + logger.error(`Action Interrupted, use ${chalk.cyan(`webpack-cli help`)} to see possible commands.`) + } } return pkgLoc ? require(scopeName).default(...args) : null; }