Skip to content

Commit

Permalink
chore: improve error on module not found (#9963)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed May 3, 2020
1 parent 71631f6 commit 8147af1
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,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

0 comments on commit 8147af1

Please sign in to comment.