Skip to content

Commit d674117

Browse files
committed
make watch plugin initialization errors look nice
1 parent 1404933 commit d674117

4 files changed

Lines changed: 62 additions & 8 deletions

File tree

packages/jest-cli/src/cli/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export async function run(maybeArgv?: Array<string>, project?: Config.Path) {
3636
} catch (error) {
3737
clearLine(process.stderr);
3838
clearLine(process.stdout);
39-
console.error(chalk.red(error.stack));
39+
console.error(chalk.red(error));
4040
exit(1);
4141
throw error;
4242
}

packages/jest-core/src/__tests__/__snapshots__/watch.test.js.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ Watch Usage
5050
]
5151
`;
5252

53+
exports[`Watch mode flows makes watch plugin initialization errors look nice 1`] = `
54+
[Error: Failed to initialize watch plugin '/Users/timseckinger/proj/jest/packages/jest-core/src/__tests__/__fixtures__/plugin_path_throws':
55+
56+
● Test suite failed to run
57+
58+
initialization failure
59+
60+
at Object.jest.doMock [as user:/Users/timseckinger/proj/jest/packages/jest-core/src/__tests__/__fixtures__/plugin_path_throws:] (watch.test.js:584:15)
61+
]
62+
`;
63+
5364
exports[`Watch mode flows shows prompts for WatchPlugins in alphabetical order 1`] = `
5465
Array [
5566
Array [

packages/jest-core/src/__tests__/watch.test.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ describe('Watch mode flows', () => {
108108
isInteractive = true;
109109
jest.doMock('jest-util/build/isInteractive', () => isInteractive);
110110
watch = require('../watch').default;
111-
const config = {roots: [], testPathIgnorePatterns: [], testRegex: []};
111+
const config = {
112+
rootDir: __dirname,
113+
roots: [],
114+
testPathIgnorePatterns: [],
115+
testRegex: [],
116+
};
112117
pipe = {write: jest.fn()};
113118
globalConfig = {watch: true};
114119
hasteMapInstances = [{on: () => {}}];
@@ -571,6 +576,31 @@ describe('Watch mode flows', () => {
571576
});
572577
});
573578

579+
it('makes watch plugin initialization errors look nice', async () => {
580+
const pluginPath = `${__dirname}/__fixtures__/plugin_path_throws`;
581+
jest.doMock(
582+
pluginPath,
583+
() => {
584+
throw new Error('initialization failure');
585+
},
586+
{virtual: true},
587+
);
588+
589+
await expect(
590+
watch(
591+
{
592+
...globalConfig,
593+
rootDir: __dirname,
594+
watchPlugins: [{config: {}, path: pluginPath}],
595+
},
596+
contexts,
597+
pipe,
598+
hasteMapInstances,
599+
stdin,
600+
),
601+
).rejects.toMatchSnapshot();
602+
});
603+
574604
it.each`
575605
ok | option
576606
${'✔︎'} | ${'bail'}

packages/jest-core/src/watch.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,25 @@ export default function watch(
171171
}
172172

173173
for (const pluginWithConfig of globalConfig.watchPlugins) {
174-
const ThirdPartyPlugin = require(pluginWithConfig.path);
175-
const plugin: WatchPlugin = new ThirdPartyPlugin({
176-
config: pluginWithConfig.config,
177-
stdin,
178-
stdout: outputStream,
179-
});
174+
let plugin: WatchPlugin;
175+
try {
176+
const ThirdPartyPlugin = require(pluginWithConfig.path);
177+
plugin = new ThirdPartyPlugin({
178+
config: pluginWithConfig.config,
179+
stdin,
180+
stdout: outputStream,
181+
});
182+
} catch (err) {
183+
return Promise.reject(
184+
new Error(
185+
`Failed to initialize watch plugin '${
186+
pluginWithConfig.path
187+
}':\n\n${formatExecError(err, contexts[0].config, {
188+
noStackTrace: false,
189+
})}`,
190+
),
191+
);
192+
}
180193
checkForConflicts(watchPluginKeys, plugin, globalConfig);
181194

182195
const hookSubscriber = hooks.getSubscriber();

0 commit comments

Comments
 (0)