Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly reprint resolver errors in watch mode #6407

Merged
merged 7 commits into from
Jul 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;