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

Add support for the "module" package.json field #2704

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dangerfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ const esModuleRegex = /^(import|export)\s(?!type(of\s|\s)(?!from)).*?$/gm;

// Ensure the use of 'use strict' on all non-ES module files
const noStrictFiles = newJsFiles.filter(filepath => {
const content = fs.readFileSync(filepath).toString();
return !esModuleRegex.test(content) && !includes(content, 'use strict');
const content = fs.readFileSync(filepath, 'utf8');
return !content.match(esModuleRegex) && !includes(content, 'use strict');
});

raiseIssueAboutPaths(fail, noStrictFiles, "'use strict'");
Expand Down
9 changes: 8 additions & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ This option is disabled by default. If you are introducing Jest to a large organ
### `browser` [boolean]
Default: `false`

Respect Browserify's [`"browser"` field](https://github.com/substack/browserify-handbook#browser-field) in `package.json` when resolving modules. Some modules export different versions based on whether they are operating in Node or a browser.
Respect the [`"browser"` field](https://github.com/substack/browserify-handbook#browser-field) in `package.json` when resolving modules. Some modules export different versions based on whether they are operating in Node or a browser.

### `bail` [boolean]
Default: `false`
Expand Down Expand Up @@ -150,6 +150,13 @@ For example, the following would create a global `__DEV__` variable set to `true

Note that, if you specify a global reference value (like an object or array) here, and some code mutates that value in the midst of running a test, that mutation will *not* be persisted across test runs for other test files.

### `module` [boolean]
Default: `false`

Respect the [`"module"` field](https://github.com/nodejs/node-eps/blob/master/002-es6-modules.md) in `package.json`. Many libraries that contain ES modules ([import/export syntax](http://www.2ality.com/2014/09/es6-modules-final.html)) use this field as a pointer to their untranspiled (ES6+) source, in addition to the ubiquitous `"main"` field that points to precompiled ES5 javascript.

_TL;DR: enable this if your project uses import/export syntax._

### `moduleFileExtensions` [array<string>]
Default: `["js", "json", "jsx", "node"]`

Expand Down
3 changes: 3 additions & 0 deletions examples/module-field/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
13 changes: 13 additions & 0 deletions examples/module-field/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
import Contents from '../sample';

describe('module field resolution', () => {
it('should return the correct file exports', () => {
expect(Contents).toBe('👏');
});
});
13 changes: 13 additions & 0 deletions examples/module-field/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"devDependencies": {
"babel-jest": "*",
"babel-preset-es2015": "*",
"jest": "*"
},
"jest": {
"module": true
},
"scripts": {
"test": "jest"
}
}
7 changes: 7 additions & 0 deletions examples/module-field/sample/correct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
export default '👏';
4 changes: 4 additions & 0 deletions examples/module-field/sample/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "wrong.js",
"module": "correct.js"
}
9 changes: 9 additions & 0 deletions examples/module-field/sample/wrong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

module.exports = '😒';
11 changes: 11 additions & 0 deletions packages/jest-config/src/__tests__/normalize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,17 @@ describe('testPathIgnorePatterns', () => {
});
});

describe('module', () => {
it('falsy module is not overwritten', () => {
const config = normalize({
module: true,
rootDir: '/root/path/foo',
});

expect(config.module).toBe(true);
});
});

describe('modulePathIgnorePatterns', () => {
it('does not normalize paths relative to rootDir', () => {
// This is a list of patterns, so we can't assume any of them are
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = ({
haste: {
providesModuleNodeModules: [],
},
module: false,
moduleDirectories: ['node_modules'],
moduleFileExtensions: ['js', 'json', 'jsx', 'node'],
moduleNameMapper: {},
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ function normalize(config: InitialConfig, argv: Object = {}) {
case 'globals':
case 'logHeapUsage':
case 'logTransformErrors':
case 'module':
case 'moduleDirectories':
case 'moduleFileExtensions':
case 'moduleLoader':
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/validConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module.exports = ({
},
logHeapUsage: true,
logTransformErrors: true,
module: false,
moduleDirectories: ['node_modules'],
moduleFileExtensions: ['js', 'json', 'jsx', 'node'],
moduleLoader: '<rootDir>',
Expand Down
17 changes: 15 additions & 2 deletions packages/jest-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ResolverConfig = {|
defaultPlatform: ?string,
extensions: Array<string>,
hasCoreModules: boolean,
module?: boolean,
moduleDirectories: Array<string>,
moduleNameMapper: ?Array<ModuleNameMapperConfig>,
modulePaths: Array<Path>,
Expand All @@ -34,6 +35,7 @@ type FindNodeModuleConfig = {|
basedir: Path,
browser?: boolean,
extensions?: Array<string>,
module?: boolean,
moduleDirectory?: Array<string>,
paths?: Array<Path>,
|};
Expand Down Expand Up @@ -69,6 +71,7 @@ class Resolver {
hasCoreModules: options.hasCoreModules === undefined
? true
: options.hasCoreModules,
module: options.module,
moduleDirectories: options.moduleDirectories || ['node_modules'],
moduleNameMapper: options.moduleNameMapper,
modulePaths: options.modulePaths,
Expand All @@ -83,13 +86,21 @@ class Resolver {
static findNodeModule(path: Path, options: FindNodeModuleConfig): ?Path {
const paths = options.paths;
try {
const resv = options.browser ? browserResolve : resolve;
return resv.sync(
return (options.browser ? browserResolve : resolve).sync(
path,
{
basedir: options.basedir,
extensions: options.extensions,
moduleDirectory: options.moduleDirectory,
packageFilter: options.module
? packageJson => {
if (packageJson.module) {
packageJson.main = packageJson.module;
}

return packageJson;
}
: x => x,
paths: paths ? (nodePaths || []).concat(paths) : nodePaths,
},
);
Expand Down Expand Up @@ -143,6 +154,7 @@ class Resolver {
basedir: dirname,
browser: this._options.browser,
extensions,
module: this._options.module,
moduleDirectory,
paths,
});
Expand Down Expand Up @@ -320,6 +332,7 @@ class Resolver {
basedir: dirname,
browser: this._options.browser,
extensions,
module: this._options.module,
moduleDirectory,
paths,
},
Expand Down
1 change: 1 addition & 0 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ class Runtime {
defaultPlatform: config.haste.defaultPlatform,
extensions: config.moduleFileExtensions.map(extension => '.' + extension),
hasCoreModules: true,
module: config.module,
moduleDirectories: config.moduleDirectories,
moduleNameMapper: getModuleNameMapper(config),
modulePaths: config.modulePaths,
Expand Down
3 changes: 3 additions & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type DefaultConfig = {|
expand: boolean,
globals: ConfigGlobals,
haste: HasteConfig,
module: boolean,
moduleDirectories: Array<string>,
moduleFileExtensions: Array<string>,
moduleNameMapper: {[key: string]: string},
Expand Down Expand Up @@ -76,6 +77,7 @@ export type Config = {|
haste: HasteConfig,
logHeapUsage: boolean,
logTransformErrors: ?boolean,
module: boolean,
moduleDirectories: Array<string>,
moduleFileExtensions: Array<string>,
moduleLoader: Path,
Expand Down Expand Up @@ -134,6 +136,7 @@ export type InitialConfig = {|
haste?: HasteConfig,
logHeapUsage?: boolean,
logTransformErrors?: ?boolean,
module?: boolean,
moduleDirectories?: Array<string>,
moduleFileExtensions?: Array<string>,
moduleLoader?: Path,
Expand Down