Skip to content

Commit fdd700f

Browse files
authored
fix: normalize setupFilesAfterEnv from user and presets (#9495)
1 parent c9127bf commit fdd700f

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
### Fixes
99

10+
- `[jest-config]` Treat `setupFilesAfterEnv` like `setupFiles` when normalizing configs against presets ([#9495](https://github.com/facebook/jest/pull/9495))
1011
- `[jest-snapshot]` Downgrade semver to v6 to support node 8 ([#9451](https://github.com/facebook/jest/pull/9451))
1112
- `[jest-transform]` Correct sourcemap behavior for transformed and instrumented code ([#9460](https://github.com/facebook/jest/pull/9460))
1213
- `[pretty-format]` Export `OldPlugin` type ([#9491](https://github.com/facebook/jest/pull/9491))

packages/jest-config/src/__tests__/normalize.test.js

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,7 @@ describe('preset', () => {
11371137
preset: 'react-native',
11381138
rootDir: '/root/path/foo',
11391139
setupFiles: ['a'],
1140+
setupFilesAfterEnv: ['a'],
11401141
transform: {a: 'a'},
11411142
},
11421143
{},
@@ -1151,6 +1152,10 @@ describe('preset', () => {
11511152
'/node_modules/a',
11521153
'/node_modules/b',
11531154
]);
1155+
expect(options.setupFilesAfterEnv.sort()).toEqual([
1156+
'/node_modules/a',
1157+
'/node_modules/b',
1158+
]);
11541159
expect(options.transform).toEqual([
11551160
['a', '/node_modules/a', {}],
11561161
['b', '/node_modules/b', {}],
@@ -1278,45 +1283,48 @@ describe('preset with globals', () => {
12781283
});
12791284
});
12801285

1281-
describe('preset without setupFiles', () => {
1282-
let Resolver;
1283-
beforeEach(() => {
1284-
Resolver = require('jest-resolve');
1285-
Resolver.findNodeModule = jest.fn(
1286-
name => path.sep + 'node_modules' + path.sep + name,
1287-
);
1288-
});
1286+
describe.each(['setupFiles', 'setupFilesAfterEnv'])(
1287+
'preset without %s',
1288+
configKey => {
1289+
let Resolver;
1290+
beforeEach(() => {
1291+
Resolver = require('jest-resolve');
1292+
Resolver.findNodeModule = jest.fn(
1293+
name => path.sep + 'node_modules' + path.sep + name,
1294+
);
1295+
});
12891296

1290-
beforeAll(() => {
1291-
jest.doMock(
1292-
'/node_modules/react-foo/jest-preset',
1293-
() => ({
1294-
moduleNameMapper: {b: 'b'},
1295-
modulePathIgnorePatterns: ['b'],
1296-
}),
1297-
{virtual: true},
1298-
);
1299-
});
1297+
beforeAll(() => {
1298+
jest.doMock(
1299+
'/node_modules/react-foo/jest-preset',
1300+
() => ({
1301+
moduleNameMapper: {b: 'b'},
1302+
modulePathIgnorePatterns: ['b'],
1303+
}),
1304+
{virtual: true},
1305+
);
1306+
});
13001307

1301-
afterAll(() => {
1302-
jest.dontMock('/node_modules/react-foo/jest-preset');
1303-
});
1308+
afterAll(() => {
1309+
jest.dontMock('/node_modules/react-foo/jest-preset');
1310+
});
13041311

1305-
it('should normalize setupFiles correctly', () => {
1306-
const {options} = normalize(
1307-
{
1308-
preset: 'react-foo',
1309-
rootDir: '/root/path/foo',
1310-
setupFiles: ['a'],
1311-
},
1312-
{},
1313-
);
1312+
it(`should normalize ${configKey} correctly`, () => {
1313+
const {options} = normalize(
1314+
{
1315+
[configKey]: ['a'],
1316+
preset: 'react-foo',
1317+
rootDir: '/root/path/foo',
1318+
},
1319+
{},
1320+
);
13141321

1315-
expect(options).toEqual(
1316-
expect.objectContaining({setupFiles: ['/node_modules/a']}),
1317-
);
1318-
});
1319-
});
1322+
expect(options).toEqual(
1323+
expect.objectContaining({[configKey]: ['/node_modules/a']}),
1324+
);
1325+
});
1326+
},
1327+
);
13201328

13211329
describe('runner', () => {
13221330
let Resolver;

packages/jest-config/src/normalize.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ const setupPreset = (
156156
if (options.setupFiles) {
157157
options.setupFiles = (preset.setupFiles || []).concat(options.setupFiles);
158158
}
159+
if (options.setupFilesAfterEnv) {
160+
options.setupFilesAfterEnv = (preset.setupFilesAfterEnv || []).concat(
161+
options.setupFilesAfterEnv,
162+
);
163+
}
159164
if (options.modulePathIgnorePatterns && preset.modulePathIgnorePatterns) {
160165
options.modulePathIgnorePatterns = preset.modulePathIgnorePatterns.concat(
161166
options.modulePathIgnorePatterns,

0 commit comments

Comments
 (0)