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

chore: improve error on module not found #9963

Merged
merged 2 commits into from May 3, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
- `[jest-circus]` [**BREAKING**] Fail tests if a test takes a done callback and have return values ([#9129](https://github.com/facebook/jest/pull/9129))
- `[jest-circus]` [**BREAKING**] Throw a proper error if a test / hook is defined asynchronously ([#8096](https://github.com/facebook/jest/pull/8096))
- `[jest-config, jest-resolve]` [**BREAKING**] Remove support for `browser` field ([#9943](https://github.com/facebook/jest/pull/9943))
- `[jest-resolve]` Show relative path from root dir for `module not found` errors ([#9963](https://github.com/facebook/jest/pull/9963))

### Chore & Maintenance

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/stackTrace.test.ts
Expand Up @@ -78,7 +78,7 @@ describe('Stack Trace', () => {

// Make sure we show Jest's jest-resolve as part of the stack trace
expect(stderr).toMatch(
/Cannot find module 'this-module-does-not-exist' from 'testError.test\.js'/,
/Cannot find module 'this-module-does-not-exist' from '__tests__\/testError\.test\.js'/,
);

expect(stderr).toMatch(
Expand Down
2 changes: 1 addition & 1 deletion e2e/resolve/__tests__/resolve.test.js
Expand Up @@ -121,7 +121,7 @@ test('should throw module not found error if the module cannot be found', () =>
expect(() => require('Test8')).toThrow(
expect.objectContaining({
code: 'MODULE_NOT_FOUND',
message: "Cannot find module 'Test8' from 'resolve.test.js'",
message: "Cannot find module 'Test8' from '__tests__/resolve.test.js'",
}),
);
});
4 changes: 2 additions & 2 deletions packages/jest-resolve/src/index.ts
Expand Up @@ -236,10 +236,10 @@ class Resolver {
// 5. Throw an error if the module could not be found. `resolve.sync` only
// produces an error based on the dirname but we have the actual current
// module name available.
const relativePath = path.relative(dirname, from);
const relativePath = path.relative(this._options.rootDir, from) || '.';

throw new ModuleNotFoundError(
`Cannot find module '${moduleName}' from '${relativePath || '.'}'`,
`Cannot find module '${moduleName}' from '${relativePath}'`,
moduleName,
);
}
Expand Down