Skip to content

Commit

Permalink
Support jest-preset.js files within Node modules. (#6185)
Browse files Browse the repository at this point in the history
* Support jest-preset.js files within Node modules.

Use resolver extensions options.

* Fix normalize tests

* Delete preset from require cache to allow multiple projects

* Prettify Configuration.md

* Add integration test for js and json presets

* Update changelog

* Update package.json

* Update package.json

* Delete require cache before requiring preset

* refactor normalize to use startsWith instead of charAt
  • Loading branch information
robin-drexler authored and cpojer committed May 22, 2018
1 parent 6fe69d1 commit 74892f0
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 7 deletions.
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

0 comments on commit 74892f0

Please sign in to comment.