From cc46dd2044b1a21cda926b4699fea3f26c15e9d8 Mon Sep 17 00:00:00 2001 From: Jo Hanna Pearce Date: Tue, 8 Oct 2019 17:07:50 +0100 Subject: [PATCH] feat(jest-config): merge preset globals with project globals --- CHANGELOG.md | 1 + .../src/__tests__/normalize.test.js | 56 +++++++++++++++++++ packages/jest-config/src/normalize.ts | 15 +++++ .../jest-haste-map/src/lib/FSEventsWatcher.ts | 1 - 4 files changed, 72 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1bf053cde4a..9e1ec0f917f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 9f085294e276..28959e5727c4 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -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(() => { diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index fea4d8da24f1..4d4c7943172a 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -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, @@ -149,6 +163,7 @@ const setupPreset = ( } mergeModuleNameMapperWithPreset(options, preset); mergeTransformWithPreset(options, preset); + mergeGlobalsWithPreset(options, preset); return {...preset, ...options}; }; diff --git a/packages/jest-haste-map/src/lib/FSEventsWatcher.ts b/packages/jest-haste-map/src/lib/FSEventsWatcher.ts index 468193e3d539..b275fd6011cd 100644 --- a/packages/jest-haste-map/src/lib/FSEventsWatcher.ts +++ b/packages/jest-haste-map/src/lib/FSEventsWatcher.ts @@ -11,7 +11,6 @@ import * as path from 'path'; import {EventEmitter} from 'events'; import anymatch, {Matcher} from 'anymatch'; import micromatch = require('micromatch'); -// eslint-disable-next-line import {Watcher} from 'fsevents'; // @ts-ignore no types import walker from 'walker';