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

Make it possible to merge transform option with preset #5505

Merged
merged 4 commits into from
Feb 17, 2018
Merged
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
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',
]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

babel-jest isn't explicitly set, so it doesn't append regenerator-runtime anymore.

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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope it is fine. This is the first time I update code that uses Flow :)

preset: InitialOptions,
optionName: string,
) => {
if (options[optionName] && preset[optionName]) {
options[optionName] = Object.assign(
{},
options[optionName],
preset[optionName],
options[optionName],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have options[optionName] twice? I realise it was the same before this patch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To preserve the order of keys in the config that is overriding preset. I had the same question initially 😃 Maybe an inline comment with explanation would help?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think so.

);
}
};

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