Skip to content

Commit

Permalink
feat(jest-config): merge preset globals with project globals
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpearce committed Oct 8, 2019
1 parent 464933f commit b5473ae
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `[expect]` Add `BigInt` support to `toBeGreaterThan`, `toBeGreaterThanOrEqual`, `toBeLessThan` and `toBeLessThanOrEqual` ([#8382](https://github.com/facebook/jest/pull/8382))
- `[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))
- `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689))
- `[jest-config]` Merge preset globals with project globals ([#9027](https://github.com/facebook/jest/pull/9027))
- `[jest-diff]` Add options for colors and symbols ([#8841](https://github.com/facebook/jest/pull/8841))
- `[jest-diff]` [**BREAKING**] Export as ECMAScript module ([#8873](https://github.com/facebook/jest/pull/8873))
- `[jest-diff]` Add `includeChangeCounts` and rename `Indicator` options ([#8881](https://github.com/facebook/jest/pull/8881))
Expand Down
56 changes: 56 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,62 @@ describe('preset', () => {
});
});

describe('preset with globals', () => {
beforeEach(() => {
const Resolver = require('jest-resolve');
Resolver.findNodeModule = jest.fn(name => {
if (name === 'global-foo/jest-preset') {
return '/node_modules/global-foo/jest-preset.json';
}

return '/node_modules/' + name;
});
jest.doMock(
'/node_modules/global-foo/jest-preset.json',
() => ({
globals: {
config: {
hereToStay: 'This should stay here',
},
},
}),
{virtual: true},
);
});

afterEach(() => {
jest.dontMock('/node_modules/global-foo/jest-preset.json');
});

test('should merge the globals preset correctly', () => {
const {options} = normalize(
{
preset: 'global-foo',
rootDir: '/root/path/foo',
globals: {
textValue: 'This is just text',
config: {
sideBySide: 'This should also live another day',
},
},
},
{},
);

expect(options).toEqual(
expect.objectContaining({
globals: {
textValue: 'This is just text',
config: {
hereToStay: 'This should stay here',
sideBySide: 'This should also live another day',
},
},
}),
);
});
});

describe('preset without setupFiles', () => {
let Resolver;
beforeEach(() => {
Expand Down
15 changes: 15 additions & 0 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ const mergeTransformWithPreset = (
}
};

const mergeGlobalsWithPreset = (
options: Config.InitialOptions,
preset: Config.InitialOptions,
) => {
if (options['globals'] && preset['globals']) {
for (const p in preset['globals']) {
options['globals'][p] = {
...preset['globals'][p],
...options['globals'][p],
};
}
}
};

const setupPreset = (
options: Config.InitialOptions,
optionsPreset: string,
Expand Down Expand Up @@ -149,6 +163,7 @@ const setupPreset = (
}
mergeModuleNameMapperWithPreset(options, preset);
mergeTransformWithPreset(options, preset);
mergeGlobalsWithPreset(options, preset);

return {...preset, ...options};
};
Expand Down

0 comments on commit b5473ae

Please sign in to comment.