Skip to content

Commit edb669d

Browse files
committed
fix: improve error on module not found
1 parent edf63d8 commit edb669d

File tree

12 files changed

+134
-3
lines changed

12 files changed

+134
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- `[jest-circus, jest-jasmine2]` Support older version of `jest-runtime` ([#9903](https://github.com/facebook/jest/pull/9903) & [#9842](https://github.com/facebook/jest/pull/9842))
1414
- `[@jest/environment]` Make sure not to reference Jest types ([#9875](https://github.com/facebook/jest/pull/9875))
1515
- `[jest-message-util]` Code frame printing should respect `--noStackTrace` flag ([#9866](https://github.com/facebook/jest/pull/9866))
16+
- `[jest-resolve]`: Improve error on module not found ([#8704](https://github.com/facebook/jest/pull/8704)
1617
- `[jest-runtime]` Support importing CJS from ESM using `import` statements ([#9850](https://github.com/facebook/jest/pull/9850))
1718
- `[jest-runtime]` Support importing parallel dynamic `import`s ([#9858](https://github.com/facebook/jest/pull/9858))
1819
- `[jest-transform]` Improve source map handling when instrumenting transformed code ([#9811](https://github.com/facebook/jest/pull/9811))

e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@ FAIL __tests__/index.js
6767
6868
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:545:17)
6969
at Object.require (index.js:10:1)
70+
at Object.require (__tests__/index.js:10:20)
7071
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`shows a proper error from deep requires 1`] = `
4+
FAIL ./test.js
5+
● Test suite failed to run
6+
7+
Cannot find module '../package' from 'node_modules/discord.js/src/index.js'
8+
9+
However, Jest was able to find:
10+
'../package.json'
11+
12+
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js'].
13+
14+
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
15+
16+
8 | 'use strict';
17+
9 |
18+
> 10 | require('discord.js');
19+
| ^
20+
11 |
21+
12 | test('dummy', () => {
22+
13 | expect(1).toBe(1);
23+
24+
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:255:17)
25+
at Object.<anonymous> (node_modules/discord.js/src/index.js:21:12)
26+
at Object.require (test.js:10:1)
27+
`;

e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ FAIL __tests__/test.js
3939
4040
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:299:11)
4141
at Object.require (index.js:8:18)
42+
at Object.require (__tests__/test.js:8:11)
4243
`;
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import path from 'path';
9+
import wrap from 'jest-snapshot-serializer-raw';
10+
import {extractSummary, run} from '../Utils';
11+
import runJest from '../runJest';
12+
13+
const dir = path.resolve(__dirname, '../require-missing-ext');
14+
15+
beforeEach(() => {
16+
run('yarn', dir);
17+
});
18+
19+
test('shows a proper error from deep requires', () => {
20+
const {stderr} = runJest(dir);
21+
const {rest} = extractSummary(stderr);
22+
23+
expect(wrap(rest)).toMatchSnapshot();
24+
});

e2e/__tests__/stackTrace.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('Stack Trace', () => {
7878

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

8484
expect(stderr).toMatch(

e2e/require-missing-ext/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"jest": {
3+
"moduleFileExtensions": [
4+
"js"
5+
],
6+
"testEnvironment": "node"
7+
},
8+
"dependencies": {
9+
"discord.js": "11.5.1"
10+
}
11+
}

e2e/require-missing-ext/test.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
require('discord.js');
11+
12+
test('dummy', () => {
13+
expect(1).toBe(1);
14+
});

e2e/require-missing-ext/yarn.lock

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
async-limiter@~1.0.0:
6+
version "1.0.0"
7+
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8"
8+
integrity sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==
9+
10+
11+
version "11.5.1"
12+
resolved "https://registry.yarnpkg.com/discord.js/-/discord.js-11.5.1.tgz#910fb9f6410328581093e044cafb661783a4d9e8"
13+
integrity sha512-tGhV5xaZXE3Z+4uXJb3hYM6gQ1NmnSxp9PClcsSAYFVRzH6AJH74040mO3afPDMWEAlj8XsoPXXTJHTxesqcGw==
14+
dependencies:
15+
long "^4.0.0"
16+
prism-media "^0.0.3"
17+
snekfetch "^3.6.4"
18+
tweetnacl "^1.0.0"
19+
ws "^6.0.0"
20+
21+
long@^4.0.0:
22+
version "4.0.0"
23+
resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
24+
integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==
25+
26+
prism-media@^0.0.3:
27+
version "0.0.3"
28+
resolved "https://registry.yarnpkg.com/prism-media/-/prism-media-0.0.3.tgz#8842d4fae804f099d3b48a9a38e3c2bab6f4855b"
29+
integrity sha512-c9KkNifSMU/iXT8FFTaBwBMr+rdVcN+H/uNv1o+CuFeTThNZNTOrQ+RgXA1yL/DeLk098duAeRPP3QNPNbhxYQ==
30+
31+
snekfetch@^3.6.4:
32+
version "3.6.4"
33+
resolved "https://registry.yarnpkg.com/snekfetch/-/snekfetch-3.6.4.tgz#d13e80a616d892f3d38daae4289f4d258a645120"
34+
integrity sha512-NjxjITIj04Ffqid5lqr7XdgwM7X61c/Dns073Ly170bPQHLm6jkmelye/eglS++1nfTWktpP6Y2bFXjdPlQqdw==
35+
36+
tweetnacl@^1.0.0:
37+
version "1.0.1"
38+
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.1.tgz#2594d42da73cd036bd0d2a54683dd35a6b55ca17"
39+
integrity sha512-kcoMoKTPYnoeS50tzoqjPY3Uv9axeuuFAZY9M/9zFnhoVvRfxz9K29IMPD7jGmt2c8SW7i3gT9WqDl2+nV7p4A==
40+
41+
ws@^6.0.0:
42+
version "6.2.1"
43+
resolved "https://registry.yarnpkg.com/ws/-/ws-6.2.1.tgz#442fdf0a47ed64f59b6a5d8ff130f4748ed524fb"
44+
integrity sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==
45+
dependencies:
46+
async-limiter "~1.0.0"

packages/jest-environment-node/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class NodeEnvironment implements JestEnvironment {
6464
global.queueMicrotask = queueMicrotask;
6565
}
6666
installCommonGlobals(global, config.globals);
67+
// Node's error-message stack size is limited at 10, but it's pretty useful
68+
// to see more than that when a test fails.
69+
global.Error.stackTraceLimit = 100;
6770
this.moduleMocker = new ModuleMocker(global);
6871

6972
const timerIdToRef = (id: number) => ({

packages/jest-resolve/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ class Resolver {
239239
// 5. Throw an error if the module could not be found. `resolve.sync` only
240240
// produces an error based on the dirname but we have the actual current
241241
// module name available.
242-
const relativePath = path.relative(dirname, from);
242+
const relativePath = path.relative(this._options.rootDir, from) || '.';
243243

244244
throw new ModuleNotFoundError(
245-
`Cannot find module '${moduleName}' from '${relativePath || '.'}'`,
245+
`Cannot find module '${moduleName}' from '${relativePath}'`,
246246
moduleName,
247247
);
248248
}

packages/jest-runner/src/runTest.ts

+3
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ async function runTestInternal(
232232
typeof environment.getVmContext === 'function' &&
233233
typeof compileFunction === 'function';
234234

235+
// Node's error-message stack size is limited at 10, but it's pretty useful
236+
// to see more than that when a test fails.
237+
Error.stackTraceLimit = 100;
235238
try {
236239
await environment.setup();
237240

0 commit comments

Comments
 (0)