Skip to content

Commit

Permalink
fix: properly reprint resolver errors in watch mode (#6407)
Browse files Browse the repository at this point in the history
* fix: properly reprint resolver errors in watch mode

* apply SimenB's golden advice

* use error.name for error name duh

* Revert "use error.name for error name duh"

This reverts commit 90386fc.

* update snapshot

* add changelog

* update snapshot
  • Loading branch information
thymikee authored and cpojer committed Jul 7, 2018
1 parent 664681a commit ff44548
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 21 additions & 8 deletions e2e/__tests__/__snapshots__/module_name_mapper.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
"
`;
8 changes: 7 additions & 1 deletion packages/jest-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
1 change: 0 additions & 1 deletion packages/jest-resolve-dependencies/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class DependencyResolver {
}
}
}

const modules = this._hasteFS.getAllFiles().map(file => ({
dependencies: this.resolve(file, options),
file,
Expand Down
48 changes: 33 additions & 15 deletions packages/jest-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;

0 comments on commit ff44548

Please sign in to comment.