Skip to content

Commit

Permalink
fix: improve error on module not found
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Aug 6, 2020
1 parent c95abca commit f1dc432
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Fixes

- `[jest-leak-detector]` Wait properly for GC runs due to changes in Node 14.7 ([#10366](https://github.com/facebook/jest/pull/10366))
- `[jest-resolve]` Improve error on module not found deep in the `require` stack ([#8704](https://github.com/facebook/jest/pull/8704)
- `[jest-worker]` Downgrade minimum node version to 10.13 ([#10352](https://github.com/facebook/jest/pull/10352))

### Chore & Maintenance
Expand Down
2 changes: 2 additions & 0 deletions e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ FAIL __tests__/index.js
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:552:17)
at Object.require (index.js:10:1)
at Object.require (__tests__/index.js:10:20)
`;

exports[`moduleNameMapper wrong configuration 1`] = `
Expand Down Expand Up @@ -72,4 +73,5 @@ FAIL __tests__/index.js
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:552:17)
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`shows a proper error from deep requires 1`] = `
FAIL ./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
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/en/configuration#modulefileextensions-arraystring
8 | 'use strict';
9 |
> 10 | require('discord.js');
| ^
11 |
12 | test('dummy', () => {
13 | expect(1).toBe(1);
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:299:11)
at Object.<anonymous> (node_modules/discord.js/src/index.js:21:12)
at Object.require (test.js:10:1)
`;
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ FAIL __tests__/test.js
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:307:11)
at Object.require (index.js:8:18)
at Object.require (__tests__/test.js:8:11)
`;
24 changes: 24 additions & 0 deletions e2e/__tests__/requireMissingExt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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 path from 'path';
import wrap from 'jest-snapshot-serializer-raw';
import {extractSummary, run} from '../Utils';
import runJest from '../runJest';

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

beforeEach(() => {
run('yarn', dir);
});

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

expect(wrap(rest)).toMatchSnapshot();
});
11 changes: 11 additions & 0 deletions e2e/require-missing-ext/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"jest": {
"moduleFileExtensions": [
"js"
],
"testEnvironment": "node"
},
"dependencies": {
"discord.js": "11.5.1"
}
}
14 changes: 14 additions & 0 deletions e2e/require-missing-ext/test.js
Original file line number Diff line number Diff line change
@@ -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);
});
46 changes: 46 additions & 0 deletions e2e/require-missing-ext/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


async-limiter@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8"
integrity sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==

discord.js@11.5.1:
version "11.5.1"
resolved "https://registry.yarnpkg.com/discord.js/-/discord.js-11.5.1.tgz#910fb9f6410328581093e044cafb661783a4d9e8"
integrity sha512-tGhV5xaZXE3Z+4uXJb3hYM6gQ1NmnSxp9PClcsSAYFVRzH6AJH74040mO3afPDMWEAlj8XsoPXXTJHTxesqcGw==
dependencies:
long "^4.0.0"
prism-media "^0.0.3"
snekfetch "^3.6.4"
tweetnacl "^1.0.0"
ws "^6.0.0"

long@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==

prism-media@^0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/prism-media/-/prism-media-0.0.3.tgz#8842d4fae804f099d3b48a9a38e3c2bab6f4855b"
integrity sha512-c9KkNifSMU/iXT8FFTaBwBMr+rdVcN+H/uNv1o+CuFeTThNZNTOrQ+RgXA1yL/DeLk098duAeRPP3QNPNbhxYQ==

snekfetch@^3.6.4:
version "3.6.4"
resolved "https://registry.yarnpkg.com/snekfetch/-/snekfetch-3.6.4.tgz#d13e80a616d892f3d38daae4289f4d258a645120"
integrity sha512-NjxjITIj04Ffqid5lqr7XdgwM7X61c/Dns073Ly170bPQHLm6jkmelye/eglS++1nfTWktpP6Y2bFXjdPlQqdw==

tweetnacl@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.1.tgz#2594d42da73cd036bd0d2a54683dd35a6b55ca17"
integrity sha512-kcoMoKTPYnoeS50tzoqjPY3Uv9axeuuFAZY9M/9zFnhoVvRfxz9K29IMPD7jGmt2c8SW7i3gT9WqDl2+nV7p4A==

ws@^6.0.0:
version "6.2.1"
resolved "https://registry.yarnpkg.com/ws/-/ws-6.2.1.tgz#442fdf0a47ed64f59b6a5d8ff130f4748ed524fb"
integrity sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==
dependencies:
async-limiter "~1.0.0"
3 changes: 3 additions & 0 deletions packages/jest-environment-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class NodeEnvironment implements JestEnvironment {
global.queueMicrotask = queueMicrotask;
}
installCommonGlobals(global, config.globals);
// Node's error-message stack size is limited at 10, but it's pretty useful
// to see more than that when a test fails.
global.Error.stackTraceLimit = 100;
this.moduleMocker = new ModuleMocker(global);

const timerIdToRef = (id: number) => ({
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,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

0 comments on commit f1dc432

Please sign in to comment.