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

fix: improve error on module not found #8704

Merged
merged 2 commits into from Sep 10, 2022
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -10,7 +10,8 @@
### Fixes

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

### Chore & Maintenance
Expand Down
6 changes: 4 additions & 2 deletions e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Expand Up @@ -42,7 +42,8 @@ exports[`moduleNameMapper wrong array configuration 1`] = `
13 |

at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:896:17)
at Object.require (index.js:10:1)"
at Object.require (index.js:10:1)
at Object.require (__tests__/index.js:10:20)"
`;

exports[`moduleNameMapper wrong configuration 1`] = `
Expand Down Expand Up @@ -71,5 +72,6 @@ exports[`moduleNameMapper wrong configuration 1`] = `
13 |

at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:896:17)
at Object.require (index.js:10:1)"
at Object.require (index.js:10:1)
at Object.require (__tests__/index.js:10:20)"
`;
32 changes: 32 additions & 0 deletions e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap
@@ -0,0 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`shows a proper error from deep requires 1`] = `
"FAIL __tests__/test.js
● Test suite failed to run

Cannot find module '../package' from 'node_modules/discord.js/src/index.js'

Require stack:
node_modules/discord.js/src/index.js
__tests__/test.js


However, Jest was able to find:
'../package.json'

You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js'].

See https://jestjs.io/docs/configuration#modulefileextensions-arraystring

8 | 'use strict';
9 |
> 10 | require('discord.js');
| ^
11 |
12 | test('dummy', () => {
13 | expect(1).toBe(1);

at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:487:11)
at Object.<anonymous> (node_modules/discord.js/src/index.js:21:12)
at Object.require (__tests__/test.js:10:1)"
`;
Expand Up @@ -38,5 +38,6 @@ exports[`show error message with matching files 1`] = `
9 |

at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:487:11)
at Object.require (index.js:8:18)"
at Object.require (index.js:8:18)
at Object.require (__tests__/test.js:8:11)"
`;
23 changes: 23 additions & 0 deletions e2e/__tests__/requireMissingExt.test.ts
@@ -0,0 +1,23 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import * as path from 'path';
import {extractSummary, runYarnInstall} from '../Utils';
import runJest from '../runJest';

const dir = path.resolve(__dirname, '../require-missing-ext');

beforeEach(() => {
runYarnInstall(dir);
});

test('shows a proper error from deep requires', () => {
const {stderr} = runJest(dir);
const {rest} = extractSummary(stderr);

expect(rest).toMatchSnapshot();
});
14 changes: 14 additions & 0 deletions e2e/require-missing-ext/__tests__/test.js
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

require('discord.js');

test('dummy', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/require-missing-ext/package.json
@@ -0,0 +1,10 @@
{
"jest": {
"moduleFileExtensions": [
"js"
]
},
"dependencies": {
"discord.js": "11.5.1"
thymikee marked this conversation as resolved.
Show resolved Hide resolved
}
}
94 changes: 94 additions & 0 deletions e2e/require-missing-ext/yarn.lock
@@ -0,0 +1,94 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 6
cacheKey: 8

"async-limiter@npm:~1.0.0":
version: 1.0.1
resolution: "async-limiter@npm:1.0.1"
checksum: 2b849695b465d93ad44c116220dee29a5aeb63adac16c1088983c339b0de57d76e82533e8e364a93a9f997f28bbfc6a92948cefc120652bd07f3b59f8d75cf2b
languageName: node
linkType: hard

"discord.js@npm:11.5.1":
version: 11.5.1
resolution: "discord.js@npm:11.5.1"
dependencies:
long: ^4.0.0
prism-media: ^0.0.3
snekfetch: ^3.6.4
tweetnacl: ^1.0.0
ws: ^6.0.0
peerDependencies:
"@discordjs/uws": ^10.149.0
bufferutil: ^4.0.0
erlpack: "*"
libsodium-wrappers: ^0.7.3
node-opus: ^0.2.7
opusscript: ^0.0.6
sodium: ^2.0.3
peerDependenciesMeta:
bufferutil:
optional: true
erlpack:
optional: true
libsodium-wrappers:
optional: true
node-opus:
optional: true
opusscript:
optional: true
sodium:
optional: true
uws:
optional: true
checksum: fdd210b1a63bff6d39eae8c79beb2f1035d16b029a361142f9148e3ba6819e685872af2435e8cfef0b71b4871f14d4bcb7a4ce9f4c957c7a4aca1a347b3229f0
languageName: node
linkType: hard

"long@npm:^4.0.0":
version: 4.0.0
resolution: "long@npm:4.0.0"
checksum: 16afbe8f749c7c849db1f4de4e2e6a31ac6e617cead3bdc4f9605cb703cd20e1e9fc1a7baba674ffcca57d660a6e5b53a9e236d7b25a295d3855cca79cc06744
languageName: node
linkType: hard

"prism-media@npm:^0.0.3":
version: 0.0.3
resolution: "prism-media@npm:0.0.3"
checksum: 45d56138bd48616d78ec2f09b77e3700c88a4e08fb159949ee0b47fe5ce9db2d68ceac4729faebada4a23b640a9f984e7dd9b4bc72b2f52666307ca9d559fcfb
languageName: node
linkType: hard

"root-workspace-0b6124@workspace:.":
version: 0.0.0-use.local
resolution: "root-workspace-0b6124@workspace:."
dependencies:
discord.js: 11.5.1
languageName: unknown
linkType: soft

"snekfetch@npm:^3.6.4":
version: 3.6.4
resolution: "snekfetch@npm:3.6.4"
checksum: e64b383a19ced759f061ecd43bc9a75bcc9b996d1909f9b94e6aaf66b6579024340c904af05aeba6261a8dd4ad9f71763f47e134c8089707d4be10fcfc682ed9
languageName: node
linkType: hard

"tweetnacl@npm:^1.0.0":
version: 1.0.3
resolution: "tweetnacl@npm:1.0.3"
checksum: e4a57cac188f0c53f24c7a33279e223618a2bfb5fea426231991652a13247bea06b081fd745d71291fcae0f4428d29beba1b984b1f1ce6f66b06a6d1ab90645c
languageName: node
linkType: hard

"ws@npm:^6.0.0":
version: 6.2.2
resolution: "ws@npm:6.2.2"
dependencies:
async-limiter: ~1.0.0
checksum: aec3154ec51477c094ac2cb5946a156e17561a581fa27005cbf22c53ac57f8d4e5f791dd4bbba6a488602cb28778c8ab7df06251d590507c3c550fd8ebeee949
languageName: node
linkType: hard
4 changes: 4 additions & 0 deletions packages/jest-environment-node/src/index.ts
Expand Up @@ -112,6 +112,10 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {

installCommonGlobals(global, projectConfig.globals);

// Node's error-message stack size is limited at 10, but it's pretty useful
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to match jsdom env

// to see more than that when a test fails.
global.Error.stackTraceLimit = 100;

if ('customExportConditions' in projectConfig.testEnvironmentOptions) {
const {customExportConditions} = projectConfig.testEnvironmentOptions;
if (
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-runner/src/runTest.ts
Expand Up @@ -273,6 +273,9 @@ async function runTestInternal(
globalConfig.coverageProvider === 'v8' &&
typeof environment.getVmContext === 'function';

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

Expand Down