-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw explicit errors for common moduleFileExtension failures (#7160)
<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. The two fields below are mandatory. --> <!-- Please remember to update CHANGELOG.md in the root of the project if you have not done so. --> ## Summary Inspired by #7158, I finally decided to throw a better error in the cases where people have custom `moduleFileExtension` resulting in more or less obscure errors. I've tackled 2 different issues here (in separate commits, happy to split them up into separate PRs if you want). 1. If you require a file without the file extension, we try to look for files matching _with_ a file extension and list them out. Also tell the user to either include file extension in the `require` or update `moduleFileExtension`. 2. If `js` is missing from `moduleFileExtension`, jest is unable to inject into the runtime. I decided to throw an explicit configuration error rather than fixing `jest-jasmine`'s `require`, as we'd also need all of our dependencies to do the same (e.g. `source-map` throws if we do `moduleFileExtension: []` now). Fixes #4025. <!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? --> ## Test plan Integration tests added <!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes UI. -->
- Loading branch information
Showing
18 changed files
with
2,228 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
e2e/__tests__/__snapshots__/resolve_no_file_extensions.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`show error message when no js moduleFileExtensions 1`] = ` | ||
"● Validation Error: | ||
moduleFileExtensions must include 'js': | ||
but instead received: | ||
[\\"jsx\\"] | ||
Please change your configuration to include 'js'. | ||
Configuration Documentation: | ||
https://jestjs.io/docs/configuration.html | ||
" | ||
`; | ||
|
||
exports[`show error message with matching files 1`] = ` | ||
"FAIL __tests__/test.js | ||
● Test suite failed to run | ||
Cannot find module './some-json-file' from 'index.js' | ||
However, Jest was able to find: | ||
'./some-json-file.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-array-string | ||
> 1 | module.exports = require('./some-json-file'); | ||
| ^ | ||
2 | | ||
at packages/jest-resolve/build/index.js:221:17 | ||
at index.js:1:18 | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. 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. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
import path from 'path'; | ||
import runJest from '../runJest'; | ||
import {cleanup, extractSummary, writeFiles} from '../Utils'; | ||
|
||
const DIR = path.resolve(__dirname, '../resolve_no_extensions-no-js'); | ||
|
||
beforeEach(() => cleanup(DIR)); | ||
afterAll(() => cleanup(DIR)); | ||
|
||
test('show error message with matching files', () => { | ||
const {status, stderr} = runJest('resolve_no_extensions'); | ||
const {rest} = extractSummary(stderr); | ||
|
||
expect(status).toBe(1); | ||
expect(rest).toMatchSnapshot(); | ||
}); | ||
|
||
test('show error message when no js moduleFileExtensions', () => { | ||
writeFiles(DIR, { | ||
'index.jsx': ` | ||
module.exports ={found: true}; | ||
`, | ||
'package.json': ` | ||
{ | ||
"jest": { | ||
"moduleFileExtensions": ["jsx"] | ||
} | ||
} | ||
`, | ||
'test.jsx': ` | ||
const m = require('../'); | ||
test('some test', () => { | ||
expect(m.found).toBe(true); | ||
}); | ||
`, | ||
}); | ||
|
||
const {status, stderr} = runJest('resolve_no_extensions-no-js'); | ||
|
||
expect(status).toBe(1); | ||
expect(stderr).toMatchSnapshot(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const m = require('../'); | ||
|
||
test('some test', () => { | ||
expect(m.found).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./some-json-file'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"jest": { | ||
"moduleFileExtensions": [ | ||
"js" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"found": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// flow-typed signature: 7c09aef8ac07163d6ef9e3f50c6bc35c | ||
// flow-typed version: a12a42a747/glob_v7.1.x/flow_>=v0.42.x | ||
|
||
declare module "glob" { | ||
declare type MinimatchOptions = {| | ||
debug?: boolean, | ||
nobrace?: boolean, | ||
noglobstar?: boolean, | ||
dot?: boolean, | ||
noext?: boolean, | ||
nocase?: boolean, | ||
nonull?: boolean, | ||
matchBase?: boolean, | ||
nocomment?: boolean, | ||
nonegate?: boolean, | ||
flipNegate?: boolean | ||
|}; | ||
|
||
declare type Options = {| | ||
...MinimatchOptions, | ||
cwd?: string, | ||
root?: string, | ||
nomount?: boolean, | ||
mark?: boolean, | ||
nosort?: boolean, | ||
stat?: boolean, | ||
silent?: boolean, | ||
strict?: boolean, | ||
cache?: { | ||
[path: string]: boolean | "DIR" | "FILE" | $ReadOnlyArray<string> | ||
}, | ||
statCache?: { | ||
[path: string]: boolean | { isDirectory(): boolean } | void | ||
}, | ||
symlinks?: { [path: string]: boolean | void }, | ||
realpathCache?: { [path: string]: string }, | ||
sync?: boolean, | ||
nounique?: boolean, | ||
nodir?: boolean, | ||
ignore?: string | $ReadOnlyArray<string>, | ||
follow?: boolean, | ||
realpath?: boolean, | ||
absolute?: boolean | ||
|}; | ||
|
||
/** | ||
* Called when an error occurs, or matches are found | ||
* err | ||
* matches: filenames found matching the pattern | ||
*/ | ||
declare type CallBack = (err: ?Error, matches: Array<string>) => void; | ||
|
||
declare class Glob extends events$EventEmitter { | ||
constructor(pattern: string): this; | ||
constructor(pattern: string, callback: CallBack): this; | ||
constructor(pattern: string, options: Options, callback: CallBack): this; | ||
|
||
minimatch: {}; | ||
options: Options; | ||
aborted: boolean; | ||
cache: { | ||
[path: string]: boolean | "DIR" | "FILE" | $ReadOnlyArray<string> | ||
}; | ||
statCache: { | ||
[path: string]: boolean | { isDirectory(): boolean } | void | ||
}; | ||
symlinks: { [path: string]: boolean | void }; | ||
realpathCache: { [path: string]: string }; | ||
found: Array<string>; | ||
|
||
pause(): void; | ||
resume(): void; | ||
abort(): void; | ||
} | ||
|
||
declare class GlobModule { | ||
Glob: Class<Glob>; | ||
|
||
(pattern: string, callback: CallBack): void; | ||
(pattern: string, options: Options, callback: CallBack): void; | ||
|
||
hasMagic(pattern: string, options?: Options): boolean; | ||
sync(pattern: string, options?: Options): Array<string>; | ||
} | ||
|
||
declare module.exports: GlobModule; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/jest-runtime/src/__tests__/__snapshots__/runtime_require_module_no_ext.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Runtime requireModule with no extension throws error pointing out file with extension 1`] = ` | ||
"Cannot find module 'RegularModuleWithWrongExt' from 'root.js' | ||
However, Jest was able to find: | ||
'./RegularModuleWithWrongExt.txt' | ||
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'node']. | ||
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string" | ||
`; |
28 changes: 28 additions & 0 deletions
28
packages/jest-runtime/src/__tests__/runtime_require_module_no_ext.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. 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'; | ||
|
||
let createRuntime; | ||
|
||
describe('Runtime requireModule with no extension', () => { | ||
beforeEach(() => { | ||
createRuntime = require('createRuntime'); | ||
}); | ||
|
||
it('throws error pointing out file with extension', async () => { | ||
const runtime = await createRuntime(__filename); | ||
|
||
expect(() => | ||
runtime.requireModuleOrMock( | ||
runtime.__mockRootPath, | ||
'RegularModuleWithWrongExt', | ||
), | ||
).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/jest-runtime/src/__tests__/test_root/RegularModuleWithWrongExt.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
some file |
Oops, something went wrong.