Skip to content
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
10 changes: 9 additions & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,15 @@ Activates notifications for test results.
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>]

Expand Down
42 changes: 42 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,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 @@ -905,6 +923,30 @@ describe('preset', () => {
}).not.toThrow();
});

test('supports .json preset files', () => {
const {options} = normalize(
{
preset: 'with-json-ext',
rootDir: '/root/path/foo',
},
{},
);

expect(options.moduleNameMapper).toEqual([['json', true]]);
});

test('supports .js preset files', () => {
const {options} = normalize(
{
preset: 'with-js-ext',
rootDir: '/root/path/foo',
},
{},
);

expect(options.moduleNameMapper).toEqual([['js', true]]);
});

test('merges with options', () => {
const {options} = normalize(
{
Expand Down
27 changes: 24 additions & 3 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,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 @@ -52,14 +52,35 @@ const setupPreset = (
let preset;
const presetPath = _replaceRootDirInPath(options.rootDir, optionsPreset);
const presetModule = Resolver.findNodeModule(
presetPath.endsWith(JSON_EXTENSION)
presetPath.charAt(0) === '.'
? presetPath
: path.join(presetPath, PRESET_NAME),
{
basedir: options.rootDir,
extensions: PRESET_EXTENSIONS,
},
);

// const foundModule = PRESET_EXTENSIONS.some(ext => {
// const presetModule = Resolver.findNodeModule(
// presetPath.charAt(0) === '.' || presetPath.endsWith(ext)
// ? presetPath
// : path.join(presetPath, PRESET_NAME + ext),
// {
// basedir: options.rootDir,
// },
// );
//
// try {
// // $FlowFixMe
// preset = (require(presetModule): InitialOptions);
// } catch (error) {
// return false;
// }
//
// return true;
// });

try {
// $FlowFixMe
preset = (require(presetModule): InitialOptions);
Expand Down