diff --git a/CHANGELOG.md b/CHANGELOG.md index 34f596769d23..db8c982b0a69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Fixes - `[jest-runner]` Force parallel runs for watch mode, to avoid TTY freeze ([#6647](https://github.com/facebook/jest/pull/6647)) +- `[jest-cli]` properly reprint resolver errors in watch mode ([#6407](https://github.com/facebook/jest/pull/6407)) ## 23.3.0 diff --git a/e2e/__tests__/__snapshots__/module_name_mapper.test.js.snap b/e2e/__tests__/__snapshots__/module_name_mapper.test.js.snap index 711efe6e6b06..df7ed898121c 100644 --- a/e2e/__tests__/__snapshots__/module_name_mapper.test.js.snap +++ b/e2e/__tests__/__snapshots__/module_name_mapper.test.js.snap @@ -13,14 +13,27 @@ exports[`moduleNameMapper wrong configuration 1`] = ` Configuration error: - Could not locate module ./style.css (mapped as no-such-module) - - Please check: - - \\"moduleNameMapper\\": { - \\"/\\\\.(css|less)$/\\": \\"no-such-module\\" - }, - \\"resolver\\": null + Could not locate module ./style.css mapped as: + no-such-module. + + Please check your configuration for these entries: + { + \\"moduleNameMapper\\": { + \\"/\\\\.(css|less)$/\\": \\"no-such-module\\" + }, + \\"resolver\\": null + } + + 8 | 'use strict'; + 9 | + > 10 | require('./style.css'); + | ^ + 11 | + 12 | module.exports = () => 'test'; + 13 | + + at packages/jest-resolve/build/index.js:400:17 + at index.js:10:1 " `; diff --git a/packages/jest-cli/src/watch.js b/packages/jest-cli/src/watch.js index 9486e44675ac..1ec6de1b1cad 100644 --- a/packages/jest-cli/src/watch.js +++ b/packages/jest-cli/src/watch.js @@ -236,7 +236,13 @@ export default function watch( outputStream, startRun, testWatcher, - }).catch(error => console.error(chalk.red(error.stack))); + }).catch(error => + // Errors thrown inside `runJest`, e.g. by resolvers, are caught here for + // continuous watch mode execution. We need to reprint them to the + // terminal and give just a little bit of extra space so they fit below + // `preRunMessagePrint` message nicely. + console.error('\n\n' + chalk.red(error)), + ); }; const onKeypress = (key: string) => { diff --git a/packages/jest-resolve-dependencies/src/index.js b/packages/jest-resolve-dependencies/src/index.js index 35f018a506fc..d36c59d69688 100644 --- a/packages/jest-resolve-dependencies/src/index.js +++ b/packages/jest-resolve-dependencies/src/index.js @@ -100,7 +100,6 @@ class DependencyResolver { } } } - const modules = this._hasteFS.getAllFiles().map(file => ({ dependencies: this.resolve(file, options), file, diff --git a/packages/jest-resolve/src/index.js b/packages/jest-resolve/src/index.js index 8c055a143fcf..6b11d8599b72 100644 --- a/packages/jest-resolve/src/index.js +++ b/packages/jest-resolve/src/index.js @@ -352,22 +352,13 @@ class Resolver { rootDir: this._options.rootDir, }); if (!module) { - const error = new Error( - chalk.red(`${chalk.bold('Configuration error')}: - -Could not locate module ${chalk.bold(moduleName)} (mapped as ${chalk.bold( - updatedName, - )}) - -Please check: - -"moduleNameMapper": { - "${regex.toString()}": "${chalk.bold(mappedModuleName)}" -}, -"resolver": ${chalk.bold(String(resolver))}`), + throw createNoMappedModuleFoundError( + moduleName, + updatedName, + mappedModuleName, + regex, + resolver, ); - error.stack = ''; - throw error; } return module; } @@ -381,4 +372,31 @@ Please check: } } +const createNoMappedModuleFoundError = ( + moduleName, + updatedName, + mappedModuleName, + regex, + resolver, +) => { + const error = new Error( + chalk.red(`${chalk.bold('Configuration error')}: + +Could not locate module ${chalk.bold(moduleName)} mapped as: +${chalk.bold(updatedName)}. + +Please check your configuration for these entries: +{ + "moduleNameMapper": { + "${regex.toString()}": "${chalk.bold(mappedModuleName)}" + }, + "resolver": ${chalk.bold(String(resolver))} +}`), + ); + + error.name = ''; + + return error; +}; + module.exports = Resolver;