From f7824805101ba8f4d77f3332ec46fc981976fa98 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 14 Feb 2020 11:33:05 +1300 Subject: [PATCH 1/5] fix(jest-types): make `ConfigGlobals` an interace --- CHANGELOG.md | 1 + packages/jest-types/src/Config.ts | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c440252bfc5..f3d77bc89835 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### Fixes - `[expect]` Handle readonly properties correctly ([#9575](https://github.com/facebook/jest/pull/9575)) +- `[@jest/types]` Make `ConfigGlobals` an interface to allow for declaration merging. ([#9570](https://github.com/facebook/jest/pull/9570)) - `[jest-config]` Treat `setupFilesAfterEnv` like `setupFiles` when normalizing configs against presets ([#9495](https://github.com/facebook/jest/pull/9495)) - `[jest-config]` Support `.mjs` config files on Windows as well ([#9558](https://github.com/facebook/jest/pull/9558)) - `[jest-config]` Verify `rootDir` and all `roots` are directories ([#9569](https://github.com/facebook/jest/pull/9569)) diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 5bb9eea84b11..846304024455 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -27,7 +27,9 @@ export type HasteConfig = { export type ReporterConfig = [string, Record]; export type TransformerConfig = [string, Record]; -export type ConfigGlobals = Record; +export interface ConfigGlobals { + [K: string]: any; +} export type DefaultOptions = { automock: boolean; From 28bb79d038ae0f4bc99eb5c262769ae095f27eb8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 13 Feb 2020 23:50:25 +0100 Subject: [PATCH 2/5] "alphabetical" --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3d77bc89835..1f66686e4a4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,6 @@ ### Fixes - `[expect]` Handle readonly properties correctly ([#9575](https://github.com/facebook/jest/pull/9575)) -- `[@jest/types]` Make `ConfigGlobals` an interface to allow for declaration merging. ([#9570](https://github.com/facebook/jest/pull/9570)) - `[jest-config]` Treat `setupFilesAfterEnv` like `setupFiles` when normalizing configs against presets ([#9495](https://github.com/facebook/jest/pull/9495)) - `[jest-config]` Support `.mjs` config files on Windows as well ([#9558](https://github.com/facebook/jest/pull/9558)) - `[jest-config]` Verify `rootDir` and all `roots` are directories ([#9569](https://github.com/facebook/jest/pull/9569)) @@ -30,6 +29,7 @@ - `[jest-snapshot]` Properly indent new snapshots in the presences of existing ones ([#9523](https://github.com/facebook/jest/pull/9523)) - `[jest-transform]` Correct sourcemap behavior for transformed and instrumented code ([#9460](https://github.com/facebook/jest/pull/9460)) - `[jest-transform]` Allow instrumentation of transformed files with weird file extensions ([#9589](https://github.com/facebook/jest/pull/9589)) +- `[@jest/types]` Make `ConfigGlobals` an interface to allow for declaration merging. ([#9570](https://github.com/facebook/jest/pull/9570)) - `[pretty-format]` Export `OldPlugin` type ([#9491](https://github.com/facebook/jest/pull/9491)) ### Chore & Maintenance From 64d25cd8dbd74a9e81a153d29ebab518db2ceed7 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 14 Feb 2020 12:08:16 +1300 Subject: [PATCH 3/5] fix(jest-types): type `ConfigGlobals` with `unknown` instead of `any` Co-Authored-By: Simen Bekkhus --- packages/jest-types/src/Config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 846304024455..dd55118c427d 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -28,7 +28,7 @@ export type ReporterConfig = [string, Record]; export type TransformerConfig = [string, Record]; export interface ConfigGlobals { - [K: string]: any; + [K: string]: unknown; } export type DefaultOptions = { From bb839f31ba0638dbb383e1ec104aca8dfb2d6231 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 16 Feb 2020 22:40:48 +1300 Subject: [PATCH 4/5] chore(jest-config): adjust `normalize` tests --- .../src/__tests__/normalize.test.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 1d6f786ee06e..ee4a6658f112 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -1251,9 +1251,11 @@ describe('preset with globals', () => { '/node_modules/global-foo/jest-preset.json', () => ({ globals: { + __DEV__: false, config: { hereToStay: 'This should stay here', }, + myString: 'hello world', }, }), {virtual: true}, @@ -1268,9 +1270,11 @@ describe('preset with globals', () => { const {options} = normalize( { globals: { + __DEV__: true, config: { sideBySide: 'This should also live another day', }, + myString: 'hello sunshine', textValue: 'This is just text', }, preset: 'global-foo', @@ -1279,17 +1283,15 @@ describe('preset with globals', () => { {}, ); - expect(options).toEqual( - expect.objectContaining({ - globals: { - config: { - hereToStay: 'This should stay here', - sideBySide: 'This should also live another day', - }, - textValue: 'This is just text', - }, - }), - ); + expect(options.globals).toEqual({ + __DEV__: true, + config: { + hereToStay: 'This should stay here', + sideBySide: 'This should also live another day', + }, + myString: 'hello sunshine', + textValue: 'This is just text', + }); }); }); From 75407fead4f1938f716d7ef31d5d6b091d894ee4 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 28 Feb 2020 15:10:07 +1300 Subject: [PATCH 5/5] feat(jest-config): use `deepmerge` to merge options --- packages/jest-config/package.json | 1 + packages/jest-config/src/normalize.ts | 8 ++------ yarn.lock | 5 +++++ 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index 6f3da60d5a31..2ecba2db62c6 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -15,6 +15,7 @@ "@jest/types": "^25.1.0", "babel-jest": "^25.1.0", "chalk": "^3.0.0", + "deepmerge": "^4.2.2", "glob": "^7.1.1", "jest-environment-jsdom": "^25.1.0", "jest-environment-node": "^25.1.0", diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index dafd3f0232e2..e20d7026b108 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -17,6 +17,7 @@ import micromatch = require('micromatch'); import {sync as realpath} from 'realpath-native'; import Resolver = require('jest-resolve'); import {replacePathSepForRegex} from 'jest-regex-util'; +import merge = require('deepmerge'); import validatePattern from './validatePattern'; import getMaxWorkers from './getMaxWorkers'; import { @@ -112,12 +113,7 @@ const mergeGlobalsWithPreset = ( preset: Config.InitialOptions, ) => { if (options['globals'] && preset['globals']) { - for (const p in preset['globals']) { - options['globals'][p] = { - ...preset['globals'][p], - ...options['globals'][p], - }; - } + options['globals'] = merge(preset['globals'], options['globals']); } }; diff --git a/yarn.lock b/yarn.lock index ea1bce583f4c..98ed8db9afe2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5221,6 +5221,11 @@ deepmerge@^3.2.0: resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-3.3.0.tgz#d3c47fd6f3a93d517b14426b0628a17b0125f5f7" integrity sha512-GRQOafGHwMHpjPx9iCvTgpu9NojZ49q794EEL94JVEw6VaeA8XTUyBKvAkOOjBX9oJNiV6G3P+T+tihFjo2TqA== +deepmerge@^4.2.2: + version "4.2.2" + resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.2.2.tgz#44d2ea3679b8f4d4ffba33f03d865fc1e7bf4955" + integrity sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg== + defaults@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/defaults/-/defaults-1.0.3.tgz#c656051e9817d9ff08ed881477f3fe4019f3ef7d"