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

Support jest-preset.js files within Node modules. #6185

Merged
merged 10 commits into from
May 22, 2018
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/examples/*/node_modules/

/integration-tests/*/node_modules
!/integration-tests/presets/json/node_modules
!/integration-tests/presets/js/node_modules
/integration-tests/transform/*/coverage
/integration-tests/transform/*/node_modules

Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

* `[jest-config]` Support jest-preset.js files within Node modules
([#6185](https://github.com/facebook/jest/pull/6185))
* `[jest-cli]` Add `--detectOpenHandles` flag which enables Jest to potentially
track down handles keeping it open after tests are complete.
([#6130](https://github.com/facebook/jest/pull/6130))
Expand Down
11 changes: 10 additions & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,16 @@ Specifies notification mode. Requires `notify: true`.
Default: `undefined`

A preset that is used as a base for Jest's configuration. A preset should point
to an npm module that exports a `jest-preset.json` module on its top level.
to an npm module that exports a `jest-preset.json` or `jest-preset.js` module at
its top level.

Presets may also be relative filesystem paths.

```json
{
"preset": "./node_modules/foo-bar/jest-preset.js"
}
```

### `projects` [array<string | ProjectConfig>]

Expand Down
21 changes: 21 additions & 0 deletions integration-tests/__tests__/presets.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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';

const runJest = require('../runJest');

test('supports json preset', () => {
const result = runJest('presets/json');
expect(result.status).toBe(0);
});

test('supports js preset', () => {
const result = runJest('presets/js');
expect(result.status).toBe(0);
});
11 changes: 11 additions & 0 deletions integration-tests/presets/js/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 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';

test('load file mapped by js preset', () => {
expect(require('./test.foo')).toEqual(42);
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions integration-tests/presets/js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"preset": "jest-preset-js"
}
}
11 changes: 11 additions & 0 deletions integration-tests/presets/json/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 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';

test('load file mapped by json preset', () => {
expect(require('./test.foo')).toEqual(42);
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions integration-tests/presets/json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"preset": "jest-preset-json"
}
}
39 changes: 36 additions & 3 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ describe('preset', () => {
beforeEach(() => {
const Resolver = require('jest-resolve');
Resolver.findNodeModule = jest.fn(name => {
if (name === 'react-native/jest-preset.json') {
if (name === 'react-native/jest-preset') {
return '/node_modules/react-native/jest-preset.json';
}
return '/node_modules/' + name;
Expand All @@ -897,6 +897,24 @@ describe('preset', () => {
}),
{virtual: true},
);
jest.mock(
'/node_modules/with-json-ext/jest-preset.json',
() => ({
moduleNameMapper: {
json: true,
},
}),
{virtual: true},
);
jest.mock(
'/node_modules/with-js-ext/jest-preset.js',
() => ({
moduleNameMapper: {
js: true,
},
}),
{virtual: true},
);
});

afterEach(() => {
Expand Down Expand Up @@ -943,6 +961,21 @@ describe('preset', () => {
}).not.toThrow();
});

test('searches for .json and .js preset files', () => {
const Resolver = require('jest-resolve');

normalize(
{
preset: 'react-native',
rootDir: '/root/path/foo',
},
{},
);

const options = Resolver.findNodeModule.mock.calls[0][1];
expect(options.extensions).toEqual(['.json', '.js']);
});

test('merges with options', () => {
const {options} = normalize(
{
Expand Down Expand Up @@ -1032,7 +1065,7 @@ describe('preset without setupFiles', () => {

beforeAll(() => {
jest.doMock(
'/node_modules/react-foo/jest-preset.json',
'/node_modules/react-foo/jest-preset',
() => {
return {
moduleNameMapper: {b: 'b'},
Expand All @@ -1044,7 +1077,7 @@ describe('preset without setupFiles', () => {
});

afterAll(() => {
jest.dontMock('/node_modules/react-foo/jest-preset.json');
jest.dontMock('/node_modules/react-foo/jest-preset');
});

it('should normalize setupFiles correctly', () => {
Expand Down
14 changes: 11 additions & 3 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import DEPRECATED_CONFIG from './deprecated';
import setFromArgv from './set_from_argv';
import VALID_CONFIG from './valid_config';
const ERROR = `${BULLET}Validation Error`;
const JSON_EXTENSION = '.json';
const PRESET_NAME = 'jest-preset' + JSON_EXTENSION;
const PRESET_EXTENSIONS = ['.json', '.js'];
const PRESET_NAME = 'jest-preset';

const createConfigError = message =>
new ValidationError(ERROR, message, DOCUMENTATION_NOTE);
Expand All @@ -64,15 +64,23 @@ const setupPreset = (
let preset;
const presetPath = replaceRootDirInPath(options.rootDir, optionsPreset);
const presetModule = Resolver.findNodeModule(
presetPath.endsWith(JSON_EXTENSION)
presetPath.startsWith('.')
? presetPath
: path.join(presetPath, PRESET_NAME),
{
basedir: options.rootDir,
extensions: PRESET_EXTENSIONS,
},
);

try {
// Force re-evaluation to support multiple projects
try {
if (presetModule) {
delete require.cache[require.resolve(presetModule)];
}
} catch (e) {}

// $FlowFixMe
preset = (require(presetModule): InitialOptions);
} catch (error) {
Expand Down