diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f9bc1036e5c..711923323d7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index c9ff36d29d38..0eeb2fed1791 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -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', () => { @@ -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'), ); @@ -938,6 +939,7 @@ describe('preset', () => { preset: 'react-native', rootDir: '/root/path/foo', setupFiles: ['a'], + transform: {a: 'a'}, }, {}, ); @@ -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'], ]); }); @@ -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', () => { @@ -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'}, @@ -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'], }, @@ -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', + ], }), ); }); diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 77f91ff48aa3..7e0827c67fc2 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -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, @@ -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); };