From 173c2e752c4ca96edb807299c7598528e89e0fdc Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 3 May 2020 20:04:04 +0200 Subject: [PATCH 1/2] chore: improve error on module not found --- CHANGELOG.md | 1 + e2e/__tests__/stackTrace.test.ts | 2 +- e2e/resolve/__tests__/resolve.test.js | 2 +- packages/jest-resolve/src/index.ts | 4 ++-- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ece9b02be96..9edcde1edc09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ### Chore & Maintenance diff --git a/e2e/__tests__/stackTrace.test.ts b/e2e/__tests__/stackTrace.test.ts index a9e571900cb2..48904bff60e5 100644 --- a/e2e/__tests__/stackTrace.test.ts +++ b/e2e/__tests__/stackTrace.test.ts @@ -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( diff --git a/e2e/resolve/__tests__/resolve.test.js b/e2e/resolve/__tests__/resolve.test.js index e3d3500d6b8c..a6c3b3e4af5c 100644 --- a/e2e/resolve/__tests__/resolve.test.js +++ b/e2e/resolve/__tests__/resolve.test.js @@ -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'", }), ); }); diff --git a/packages/jest-resolve/src/index.ts b/packages/jest-resolve/src/index.ts index 1efebd10b974..c253cfa8aa71 100644 --- a/packages/jest-resolve/src/index.ts +++ b/packages/jest-resolve/src/index.ts @@ -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, ); } From dc06ea8f2d9e643a4bfab6072a6672eb3b3a0aa6 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 3 May 2020 20:05:08 +0200 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9edcde1edc09..06e9c8af0204 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +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 +- `[jest-resolve]` Show relative path from root dir for `module not found` errors ([#9963](https://github.com/facebook/jest/pull/9963)) ### Chore & Maintenance