Skip to content

Commit

Permalink
fix: normalize setupFilesAfterEnv from user and presets (#9495)
Browse files Browse the repository at this point in the history
  • Loading branch information
staylor authored Feb 2, 2020
1 parent c9127bf commit fdd700f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

### Fixes

- `[jest-config]` Treat `setupFilesAfterEnv` like `setupFiles` when normalizing configs against presets ([#9495](https://github.com/facebook/jest/pull/9495))
- `[jest-snapshot]` Downgrade semver to v6 to support node 8 ([#9451](https://github.com/facebook/jest/pull/9451))
- `[jest-transform]` Correct sourcemap behavior for transformed and instrumented code ([#9460](https://github.com/facebook/jest/pull/9460))
- `[pretty-format]` Export `OldPlugin` type ([#9491](https://github.com/facebook/jest/pull/9491))
Expand Down
78 changes: 43 additions & 35 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,7 @@ describe('preset', () => {
preset: 'react-native',
rootDir: '/root/path/foo',
setupFiles: ['a'],
setupFilesAfterEnv: ['a'],
transform: {a: 'a'},
},
{},
Expand All @@ -1151,6 +1152,10 @@ describe('preset', () => {
'/node_modules/a',
'/node_modules/b',
]);
expect(options.setupFilesAfterEnv.sort()).toEqual([
'/node_modules/a',
'/node_modules/b',
]);
expect(options.transform).toEqual([
['a', '/node_modules/a', {}],
['b', '/node_modules/b', {}],
Expand Down Expand Up @@ -1278,45 +1283,48 @@ describe('preset with globals', () => {
});
});

describe('preset without setupFiles', () => {
let Resolver;
beforeEach(() => {
Resolver = require('jest-resolve');
Resolver.findNodeModule = jest.fn(
name => path.sep + 'node_modules' + path.sep + name,
);
});
describe.each(['setupFiles', 'setupFilesAfterEnv'])(
'preset without %s',
configKey => {
let Resolver;
beforeEach(() => {
Resolver = require('jest-resolve');
Resolver.findNodeModule = jest.fn(
name => path.sep + 'node_modules' + path.sep + name,
);
});

beforeAll(() => {
jest.doMock(
'/node_modules/react-foo/jest-preset',
() => ({
moduleNameMapper: {b: 'b'},
modulePathIgnorePatterns: ['b'],
}),
{virtual: true},
);
});
beforeAll(() => {
jest.doMock(
'/node_modules/react-foo/jest-preset',
() => ({
moduleNameMapper: {b: 'b'},
modulePathIgnorePatterns: ['b'],
}),
{virtual: true},
);
});

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

it('should normalize setupFiles correctly', () => {
const {options} = normalize(
{
preset: 'react-foo',
rootDir: '/root/path/foo',
setupFiles: ['a'],
},
{},
);
it(`should normalize ${configKey} correctly`, () => {
const {options} = normalize(
{
[configKey]: ['a'],
preset: 'react-foo',
rootDir: '/root/path/foo',
},
{},
);

expect(options).toEqual(
expect.objectContaining({setupFiles: ['/node_modules/a']}),
);
});
});
expect(options).toEqual(
expect.objectContaining({[configKey]: ['/node_modules/a']}),
);
});
},
);

describe('runner', () => {
let Resolver;
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ const setupPreset = (
if (options.setupFiles) {
options.setupFiles = (preset.setupFiles || []).concat(options.setupFiles);
}
if (options.setupFilesAfterEnv) {
options.setupFilesAfterEnv = (preset.setupFilesAfterEnv || []).concat(
options.setupFilesAfterEnv,
);
}
if (options.modulePathIgnorePatterns && preset.modulePathIgnorePatterns) {
options.modulePathIgnorePatterns = preset.modulePathIgnorePatterns.concat(
options.modulePathIgnorePatterns,
Expand Down

0 comments on commit fdd700f

Please sign in to comment.