Skip to content

Commit b096c16

Browse files
catfirepartySimenB
authored andcommitted
feat(jest-config): merge preset globals with project globals (#9027)
1 parent e3311ed commit b096c16

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- `[expect, jest-matcher-utils]` Display change counts in annotation lines ([#9035](https://github.com/facebook/jest/pull/9035))
1010
- `[jest-config]` Throw the full error message and stack when a Jest preset is missing a dependency ([#8924](https://github.com/facebook/jest/pull/8924))
1111
- `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689))
12+
- `[jest-config]` Merge preset globals with project globals ([#9027](https://github.com/facebook/jest/pull/9027))
1213
- `[jest-diff]` Add options for colors and symbols ([#8841](https://github.com/facebook/jest/pull/8841))
1314
- `[jest-diff]` [**BREAKING**] Export as ECMAScript module ([#8873](https://github.com/facebook/jest/pull/8873))
1415
- `[jest-diff]` Add `includeChangeCounts` and rename `Indicator` options ([#8881](https://github.com/facebook/jest/pull/8881))

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,62 @@ describe('preset', () => {
12191219
});
12201220
});
12211221

1222+
describe('preset with globals', () => {
1223+
beforeEach(() => {
1224+
const Resolver = require('jest-resolve');
1225+
Resolver.findNodeModule = jest.fn(name => {
1226+
if (name === 'global-foo/jest-preset') {
1227+
return '/node_modules/global-foo/jest-preset.json';
1228+
}
1229+
1230+
return '/node_modules/' + name;
1231+
});
1232+
jest.doMock(
1233+
'/node_modules/global-foo/jest-preset.json',
1234+
() => ({
1235+
globals: {
1236+
config: {
1237+
hereToStay: 'This should stay here',
1238+
},
1239+
},
1240+
}),
1241+
{virtual: true},
1242+
);
1243+
});
1244+
1245+
afterEach(() => {
1246+
jest.dontMock('/node_modules/global-foo/jest-preset.json');
1247+
});
1248+
1249+
test('should merge the globals preset correctly', () => {
1250+
const {options} = normalize(
1251+
{
1252+
preset: 'global-foo',
1253+
rootDir: '/root/path/foo',
1254+
globals: {
1255+
textValue: 'This is just text',
1256+
config: {
1257+
sideBySide: 'This should also live another day',
1258+
},
1259+
},
1260+
},
1261+
{},
1262+
);
1263+
1264+
expect(options).toEqual(
1265+
expect.objectContaining({
1266+
globals: {
1267+
textValue: 'This is just text',
1268+
config: {
1269+
hereToStay: 'This should stay here',
1270+
sideBySide: 'This should also live another day',
1271+
},
1272+
},
1273+
}),
1274+
);
1275+
});
1276+
});
1277+
12221278
describe('preset without setupFiles', () => {
12231279
let Resolver;
12241280
beforeEach(() => {

packages/jest-config/src/normalize.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ const mergeTransformWithPreset = (
7373
}
7474
};
7575

76+
const mergeGlobalsWithPreset = (
77+
options: Config.InitialOptions,
78+
preset: Config.InitialOptions,
79+
) => {
80+
if (options['globals'] && preset['globals']) {
81+
for (const p in preset['globals']) {
82+
options['globals'][p] = {
83+
...preset['globals'][p],
84+
...options['globals'][p],
85+
};
86+
}
87+
}
88+
};
89+
7690
const setupPreset = (
7791
options: Config.InitialOptions,
7892
optionsPreset: string,
@@ -149,6 +163,7 @@ const setupPreset = (
149163
}
150164
mergeModuleNameMapperWithPreset(options, preset);
151165
mergeTransformWithPreset(options, preset);
166+
mergeGlobalsWithPreset(options, preset);
152167

153168
return {...preset, ...options};
154169
};

0 commit comments

Comments
 (0)