Skip to content

Commit cbf0992

Browse files
authored
fix: improve error on module not found (#8704)
1 parent ac57282 commit cbf0992

File tree

10 files changed

+188
-4
lines changed

10 files changed

+188
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
### Fixes
1111

1212
- `[jest-core]` Capture execError during `TestScheduler.scheduleTests` and dispatch to reporters ([#13203](https://github.com/facebook/jest/pull/13203))
13-
- `[jest-resolver]` Make sure to resolve module paths after looking at `exports` ([#13242](https://github.com/facebook/jest/pull/13242))
13+
- `[jest-resolve]` Make sure to resolve module paths after looking at `exports` ([#13242](https://github.com/facebook/jest/pull/13242))
14+
- `[jest-resolve]` Improve error on module not found deep in the `require` stack ([#8704](https://github.com/facebook/jest/pull/8704)
1415
- `[jest-snapshot]` Fix typings of snapshot matchers ([#13240])(https://github.com/facebook/jest/pull/13240))
1516

1617
### Chore & Maintenance

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ exports[`moduleNameMapper wrong array configuration 1`] = `
4242
13 |
4343
4444
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:896:17)
45-
at Object.require (index.js:10:1)"
45+
at Object.require (index.js:10:1)
46+
at Object.require (__tests__/index.js:10:20)"
4647
`;
4748

4849
exports[`moduleNameMapper wrong configuration 1`] = `
@@ -71,5 +72,6 @@ exports[`moduleNameMapper wrong configuration 1`] = `
7172
13 |
7273
7374
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:896:17)
74-
at Object.require (index.js:10:1)"
75+
at Object.require (index.js:10:1)
76+
at Object.require (__tests__/index.js:10:20)"
7577
`;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`shows a proper error from deep requires 1`] = `
4+
"FAIL __tests__/test.js
5+
● Test suite failed to run
6+
7+
Cannot find module '../package' from 'node_modules/discord.js/src/index.js'
8+
9+
Require stack:
10+
node_modules/discord.js/src/index.js
11+
__tests__/test.js
12+
13+
14+
However, Jest was able to find:
15+
'../package.json'
16+
17+
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js'].
18+
19+
See https://jestjs.io/docs/configuration#modulefileextensions-arraystring
20+
21+
8 | 'use strict';
22+
9 |
23+
> 10 | require('discord.js');
24+
| ^
25+
11 |
26+
12 | test('dummy', () => {
27+
13 | expect(1).toBe(1);
28+
29+
at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:487:11)
30+
at Object.<anonymous> (node_modules/discord.js/src/index.js:21:12)
31+
at Object.require (__tests__/test.js:10:1)"
32+
`;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ exports[`show error message with matching files 1`] = `
3838
9 |
3939
4040
at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:487:11)
41-
at Object.require (index.js:8:18)"
41+
at Object.require (index.js:8:18)
42+
at Object.require (__tests__/test.js:8:11)"
4243
`;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 * as path from 'path';
9+
import {extractSummary, runYarnInstall} from '../Utils';
10+
import runJest from '../runJest';
11+
12+
const dir = path.resolve(__dirname, '../require-missing-ext');
13+
14+
beforeEach(() => {
15+
runYarnInstall(dir);
16+
});
17+
18+
test('shows a proper error from deep requires', () => {
19+
const {stderr} = runJest(dir);
20+
const {rest} = extractSummary(stderr);
21+
22+
expect(rest).toMatchSnapshot();
23+
});
Lines changed: 14 additions & 0 deletions
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+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"jest": {
3+
"moduleFileExtensions": [
4+
"js"
5+
]
6+
},
7+
"dependencies": {
8+
"discord.js": "11.5.1"
9+
}
10+
}

e2e/require-missing-ext/yarn.lock

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# This file is generated by running "yarn install" inside your project.
2+
# Manual changes might be lost - proceed with caution!
3+
4+
__metadata:
5+
version: 6
6+
cacheKey: 8
7+
8+
"async-limiter@npm:~1.0.0":
9+
version: 1.0.1
10+
resolution: "async-limiter@npm:1.0.1"
11+
checksum: 2b849695b465d93ad44c116220dee29a5aeb63adac16c1088983c339b0de57d76e82533e8e364a93a9f997f28bbfc6a92948cefc120652bd07f3b59f8d75cf2b
12+
languageName: node
13+
linkType: hard
14+
15+
"discord.js@npm:11.5.1":
16+
version: 11.5.1
17+
resolution: "discord.js@npm:11.5.1"
18+
dependencies:
19+
long: ^4.0.0
20+
prism-media: ^0.0.3
21+
snekfetch: ^3.6.4
22+
tweetnacl: ^1.0.0
23+
ws: ^6.0.0
24+
peerDependencies:
25+
"@discordjs/uws": ^10.149.0
26+
bufferutil: ^4.0.0
27+
erlpack: "*"
28+
libsodium-wrappers: ^0.7.3
29+
node-opus: ^0.2.7
30+
opusscript: ^0.0.6
31+
sodium: ^2.0.3
32+
peerDependenciesMeta:
33+
bufferutil:
34+
optional: true
35+
erlpack:
36+
optional: true
37+
libsodium-wrappers:
38+
optional: true
39+
node-opus:
40+
optional: true
41+
opusscript:
42+
optional: true
43+
sodium:
44+
optional: true
45+
uws:
46+
optional: true
47+
checksum: fdd210b1a63bff6d39eae8c79beb2f1035d16b029a361142f9148e3ba6819e685872af2435e8cfef0b71b4871f14d4bcb7a4ce9f4c957c7a4aca1a347b3229f0
48+
languageName: node
49+
linkType: hard
50+
51+
"long@npm:^4.0.0":
52+
version: 4.0.0
53+
resolution: "long@npm:4.0.0"
54+
checksum: 16afbe8f749c7c849db1f4de4e2e6a31ac6e617cead3bdc4f9605cb703cd20e1e9fc1a7baba674ffcca57d660a6e5b53a9e236d7b25a295d3855cca79cc06744
55+
languageName: node
56+
linkType: hard
57+
58+
"prism-media@npm:^0.0.3":
59+
version: 0.0.3
60+
resolution: "prism-media@npm:0.0.3"
61+
checksum: 45d56138bd48616d78ec2f09b77e3700c88a4e08fb159949ee0b47fe5ce9db2d68ceac4729faebada4a23b640a9f984e7dd9b4bc72b2f52666307ca9d559fcfb
62+
languageName: node
63+
linkType: hard
64+
65+
"root-workspace-0b6124@workspace:.":
66+
version: 0.0.0-use.local
67+
resolution: "root-workspace-0b6124@workspace:."
68+
dependencies:
69+
discord.js: 11.5.1
70+
languageName: unknown
71+
linkType: soft
72+
73+
"snekfetch@npm:^3.6.4":
74+
version: 3.6.4
75+
resolution: "snekfetch@npm:3.6.4"
76+
checksum: e64b383a19ced759f061ecd43bc9a75bcc9b996d1909f9b94e6aaf66b6579024340c904af05aeba6261a8dd4ad9f71763f47e134c8089707d4be10fcfc682ed9
77+
languageName: node
78+
linkType: hard
79+
80+
"tweetnacl@npm:^1.0.0":
81+
version: 1.0.3
82+
resolution: "tweetnacl@npm:1.0.3"
83+
checksum: e4a57cac188f0c53f24c7a33279e223618a2bfb5fea426231991652a13247bea06b081fd745d71291fcae0f4428d29beba1b984b1f1ce6f66b06a6d1ab90645c
84+
languageName: node
85+
linkType: hard
86+
87+
"ws@npm:^6.0.0":
88+
version: 6.2.2
89+
resolution: "ws@npm:6.2.2"
90+
dependencies:
91+
async-limiter: ~1.0.0
92+
checksum: aec3154ec51477c094ac2cb5946a156e17561a581fa27005cbf22c53ac57f8d4e5f791dd4bbba6a488602cb28778c8ab7df06251d590507c3c550fd8ebeee949
93+
languageName: node
94+
linkType: hard

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
112112

113113
installCommonGlobals(global, projectConfig.globals);
114114

115+
// Node's error-message stack size is limited at 10, but it's pretty useful
116+
// to see more than that when a test fails.
117+
global.Error.stackTraceLimit = 100;
118+
115119
if ('customExportConditions' in projectConfig.testEnvironmentOptions) {
116120
const {customExportConditions} = projectConfig.testEnvironmentOptions;
117121
if (

packages/jest-runner/src/runTest.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ async function runTestInternal(
273273
globalConfig.coverageProvider === 'v8' &&
274274
typeof environment.getVmContext === 'function';
275275

276+
// Node's error-message stack size is limited at 10, but it's pretty useful
277+
// to see more than that when a test fails.
278+
Error.stackTraceLimit = 100;
276279
try {
277280
await environment.setup();
278281

0 commit comments

Comments
 (0)