Skip to content

Commit

Permalink
Make it possible to merge transform option with preset (#5505)
Browse files Browse the repository at this point in the history
* Make it possible to merge `transform` options and preset

TBD

* Tests: Refactor tests for normalize module from jest-config

* Changelog: Add entry for #5505 PR for jest-config

* lint changelog
  • Loading branch information
gziolo authored and cpojer committed Feb 17, 2018
1 parent 3a86707 commit e89de4e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

### Fixes

* `[jest-cli]` Don't skip matchers for exact files ([#5582](https://github.com/facebook/jest/pull/5582))
* `[docs]` Update discord links ([#5586](https://github.com/facebook/jest/pull/5586))
* `[jest-cli]` Don't skip matchers for exact files
([#5582](https://github.com/facebook/jest/pull/5582))
* `[docs]` Update discord links
([#5586](https://github.com/facebook/jest/pull/5586))
* `[jest-runtime]` Align handling of testRegex on Windows between searching for
tests and instrumentation checks
([#5560](https://github.com/facebook/jest/pull/5560))
* `[jest-config]` Make it possible to merge `transform` option with preset
([#5505](https://github.com/facebook/jest/pull/5505))

### Features

Expand Down
54 changes: 46 additions & 8 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,19 +875,20 @@ describe('preset', () => {
}
return '/node_modules/' + name;
});
jest.mock(
jest.doMock(
'/node_modules/react-native/jest-preset.json',
() => ({
moduleNameMapper: {b: 'b'},
modulePathIgnorePatterns: ['b'],
setupFiles: ['b'],
transform: {b: 'b'},
}),
{virtual: true},
);
});

afterEach(() => {
jest.unmock('/node_modules/react-native/jest-preset.json');
jest.dontMock('/node_modules/react-native/jest-preset.json');
});

test('throws when preset not found', () => {
Expand All @@ -903,7 +904,7 @@ describe('preset', () => {
});

test('throws when preset is invalid', () => {
jest.mock('/node_modules/react-native/jest-preset.json', () =>
jest.doMock('/node_modules/react-native/jest-preset.json', () =>
require.requireActual('./jest-preset.json'),
);

Expand Down Expand Up @@ -938,6 +939,7 @@ describe('preset', () => {
preset: 'react-native',
rootDir: '/root/path/foo',
setupFiles: ['a'],
transform: {a: 'a'},
},
{},
);
Expand All @@ -947,7 +949,10 @@ describe('preset', () => {
expect(options.setupFiles.sort()).toEqual([
'/node_modules/a',
'/node_modules/b',
'/node_modules/regenerator-runtime/runtime',
]);
expect(options.transform).toEqual([
['a', '/node_modules/a'],
['b', '/node_modules/b'],
]);
});

Expand Down Expand Up @@ -976,6 +981,32 @@ describe('preset', () => {
['a', 'aa'],
]);
});

test('merges with options and transform preset is overridden by options', () => {
/* eslint-disable sort-keys */
const transform = {
e: 'ee',
b: 'bb',
c: 'cc',
a: 'aa',
};
/* eslint-disable sort-keys */
const {options} = normalize(
{
preset: 'react-native',
rootDir: '/root/path/foo',
transform,
},
{},
);

expect(options.transform).toEqual([
['e', '/node_modules/ee'],
['b', '/node_modules/bb'],
['c', '/node_modules/cc'],
['a', '/node_modules/aa'],
]);
});
});

describe('preset without setupFiles', () => {
Expand All @@ -988,8 +1019,8 @@ describe('preset without setupFiles', () => {
});

beforeAll(() => {
jest.mock(
'/node_modules/react-native/jest-preset.json',
jest.doMock(
'/node_modules/react-foo/jest-preset.json',
() => {
return {
moduleNameMapper: {b: 'b'},
Expand All @@ -1000,10 +1031,14 @@ describe('preset without setupFiles', () => {
);
});

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

it('should normalize setupFiles correctly', () => {
const {options} = normalize(
{
preset: 'react-native',
preset: 'react-foo',
rootDir: '/root/path/foo',
setupFiles: ['a'],
},
Expand All @@ -1012,7 +1047,10 @@ describe('preset without setupFiles', () => {

expect(options).toEqual(
expect.objectContaining({
setupFiles: expect.arrayContaining(['/node_modules/a']),
setupFiles: [
'/node_modules/regenerator-runtime/runtime',
'/node_modules/a',
],
}),
);
});
Expand Down
25 changes: 17 additions & 8 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ const PRESET_NAME = 'jest-preset' + JSON_EXTENSION;
const createConfigError = message =>
new ValidationError(ERROR, message, DOCUMENTATION_NOTE);

const mergeOptionWithPreset = (
options: InitialOptions,
preset: InitialOptions,
optionName: string,
) => {
if (options[optionName] && preset[optionName]) {
options[optionName] = Object.assign(
{},
options[optionName],
preset[optionName],
options[optionName],
);
}
};

const setupPreset = (
options: InitialOptions,
optionsPreset: string,
Expand Down Expand Up @@ -81,14 +96,8 @@ const setupPreset = (
options.modulePathIgnorePatterns,
);
}
if (options.moduleNameMapper && preset.moduleNameMapper) {
options.moduleNameMapper = Object.assign(
{},
options.moduleNameMapper,
preset.moduleNameMapper,
options.moduleNameMapper,
);
}
mergeOptionWithPreset(options, preset, 'moduleNameMapper');
mergeOptionWithPreset(options, preset, 'transform');

return Object.assign({}, preset, options);
};
Expand Down

0 comments on commit e89de4e

Please sign in to comment.