From 444afa301065a78d252d5358b2b79ebc7635855a Mon Sep 17 00:00:00 2001 From: Tim Seckinger Date: Mon, 6 May 2019 00:22:07 +0200 Subject: [PATCH] make watch plugin initialization errors look nice --- .../__snapshots__/watch.test.js.snap | 11 +++++++ .../jest-core/src/__tests__/watch.test.js | 32 ++++++++++++++++++- packages/jest-core/src/watch.ts | 25 +++++++++++---- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/packages/jest-core/src/__tests__/__snapshots__/watch.test.js.snap b/packages/jest-core/src/__tests__/__snapshots__/watch.test.js.snap index 85d1828379c5..70a6fe598f14 100644 --- a/packages/jest-core/src/__tests__/__snapshots__/watch.test.js.snap +++ b/packages/jest-core/src/__tests__/__snapshots__/watch.test.js.snap @@ -50,6 +50,17 @@ Watch Usage ] `; +exports[`Watch mode flows makes watch plugin initialization errors look nice 1`] = ` +[Error: Failed to initialize watch plugin '/Users/timseckinger/proj/jest/packages/jest-core/src/__tests__/__fixtures__/plugin_path_throws': + + ● Test suite failed to run + + initialization failure + + at Object.jest.doMock [as user:/Users/timseckinger/proj/jest/packages/jest-core/src/__tests__/__fixtures__/plugin_path_throws:] (watch.test.js:584:15) +] +`; + exports[`Watch mode flows shows prompts for WatchPlugins in alphabetical order 1`] = ` Array [ Array [ diff --git a/packages/jest-core/src/__tests__/watch.test.js b/packages/jest-core/src/__tests__/watch.test.js index 3d480a29fc23..8a9e7a0968f4 100644 --- a/packages/jest-core/src/__tests__/watch.test.js +++ b/packages/jest-core/src/__tests__/watch.test.js @@ -108,7 +108,12 @@ describe('Watch mode flows', () => { isInteractive = true; jest.doMock('jest-util/build/isInteractive', () => isInteractive); watch = require('../watch').default; - const config = {roots: [], testPathIgnorePatterns: [], testRegex: []}; + const config = { + rootDir: __dirname, + roots: [], + testPathIgnorePatterns: [], + testRegex: [], + }; pipe = {write: jest.fn()}; globalConfig = {watch: true}; hasteMapInstances = [{on: () => {}}]; @@ -571,6 +576,31 @@ describe('Watch mode flows', () => { }); }); + it('makes watch plugin initialization errors look nice', async () => { + const pluginPath = `${__dirname}/__fixtures__/plugin_path_throws`; + jest.doMock( + pluginPath, + () => { + throw new Error('initialization failure'); + }, + {virtual: true}, + ); + + await expect( + watch( + { + ...globalConfig, + rootDir: __dirname, + watchPlugins: [{config: {}, path: pluginPath}], + }, + contexts, + pipe, + hasteMapInstances, + stdin, + ), + ).rejects.toMatchSnapshot(); + }); + it.each` ok | option ${'✔︎'} | ${'bail'} diff --git a/packages/jest-core/src/watch.ts b/packages/jest-core/src/watch.ts index 2efe8e05b165..5a4c9a891334 100644 --- a/packages/jest-core/src/watch.ts +++ b/packages/jest-core/src/watch.ts @@ -171,12 +171,25 @@ export default function watch( } for (const pluginWithConfig of globalConfig.watchPlugins) { - const ThirdPartyPlugin = require(pluginWithConfig.path); - const plugin: WatchPlugin = new ThirdPartyPlugin({ - config: pluginWithConfig.config, - stdin, - stdout: outputStream, - }); + let plugin: WatchPlugin; + try { + const ThirdPartyPlugin = require(pluginWithConfig.path); + plugin = new ThirdPartyPlugin({ + config: pluginWithConfig.config, + stdin, + stdout: outputStream, + }); + } catch (err) { + return Promise.reject( + new Error( + `Failed to initialize watch plugin '${ + pluginWithConfig.path + }':\n\n${formatExecError(err, contexts[0].config, { + noStackTrace: false, + })}`, + ), + ); + } checkForConflicts(watchPluginKeys, plugin, globalConfig); const hookSubscriber = hooks.getSubscriber();