Skip to content

Commit

Permalink
make watch plugin initialization errors look nice
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed May 5, 2019
1 parent 1404933 commit d674117
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/jest-cli/src/cli/index.ts
Expand Up @@ -36,7 +36,7 @@ export async function run(maybeArgv?: Array<string>, project?: Config.Path) {
} catch (error) {
clearLine(process.stderr);
clearLine(process.stdout);
console.error(chalk.red(error.stack));
console.error(chalk.red(error));
exit(1);
throw error;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/jest-core/src/__tests__/__snapshots__/watch.test.js.snap
Expand Up @@ -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 [
Expand Down
32 changes: 31 additions & 1 deletion packages/jest-core/src/__tests__/watch.test.js
Expand Up @@ -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: () => {}}];
Expand Down Expand Up @@ -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'}
Expand Down
25 changes: 19 additions & 6 deletions packages/jest-core/src/watch.ts
Expand Up @@ -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();
Expand Down

0 comments on commit d674117

Please sign in to comment.