From 5a3ac2a9cf22f02f5b44875ed36bfe0234a5342d Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Fri, 3 Jul 2020 11:47:11 +0200 Subject: [PATCH] Add support for `recursiveDenylist` option as an alternative to `recursiveBlacklist` Related to #10235 --- CHANGELOG.md | 1 + packages/jest-validate/README.md | 2 +- .../src/__tests__/validate.test.ts | 38 ++++++++++++++++++- packages/jest-validate/src/defaultConfig.ts | 2 +- packages/jest-validate/src/exampleConfig.ts | 2 +- packages/jest-validate/src/types.ts | 1 + packages/jest-validate/src/validate.ts | 24 ++++++++---- 7 files changed, 57 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6075c2ceb399..e3b9eae802d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - `[expect]` Match symbols and bigints in `any()` ([#10223](https://github.com/facebook/jest/pull/10223)) - `[jest-snapshot]` Strip added indentation for inline error snapshots ([#10217](https://github.com/facebook/jest/pull/10217)) +- `[jest-validate]` Add support for `recursiveDenylist` option as an alternative to `recursiveBlacklist` ([#10236](https://github.com/facebook/jest/pull/10236)) ### Chore & Maintenance diff --git a/packages/jest-validate/README.md b/packages/jest-validate/README.md index efe749a25838..7622c2618bdb 100644 --- a/packages/jest-validate/README.md +++ b/packages/jest-validate/README.md @@ -62,7 +62,7 @@ Almost anything can be overwritten to suite your needs. ### Options -- `recursiveBlacklist` – optional array of string keyPaths that should be excluded from deep (recursive) validation. +- `recursiveDenylist` – optional array of string keyPaths that should be excluded from deep (recursive) validation. - `comment` – optional string to be rendered below error/warning message. - `condition` – an optional function with validation condition. - `deprecate`, `error`, `unknown` – optional functions responsible for displaying warning and error messages. diff --git a/packages/jest-validate/src/__tests__/validate.test.ts b/packages/jest-validate/src/__tests__/validate.test.ts index eb5af529bb2b..e241c7140261 100644 --- a/packages/jest-validate/src/__tests__/validate.test.ts +++ b/packages/jest-validate/src/__tests__/validate.test.ts @@ -101,7 +101,7 @@ test.each([ }, ); -test('respects blacklist', () => { +test('respects recursiveBlacklist', () => { const warn = console.warn; console.warn = jest.fn(); const config = { @@ -135,6 +135,40 @@ test('respects blacklist', () => { console.warn = warn; }); +test('respects recursiveDenylist', () => { + const warn = console.warn; + console.warn = jest.fn(); + const config = { + something: { + nested: { + some_random_key: 'value', + some_random_key2: 'value2', + }, + }, + }; + const exampleConfig = { + something: { + nested: { + test: true, + }, + }, + }; + + validate(config, {exampleConfig}); + + expect(console.warn).toBeCalled(); + + console.warn.mockReset(); + + validate(config, { + exampleConfig, + recursiveDenylist: ['something.nested'], + }); + + expect(console.warn).not.toBeCalled(); + console.warn = warn; +}); + test('displays warning for unknown config options', () => { const config = {unkwon: {}}; const validConfig = {unknown: 'string'}; @@ -292,7 +326,7 @@ test('Comments in config JSON using "//" key are not warned', () => { validate(config, { exampleConfig: validConfig, - recursiveBlacklist: ['myCustomKey' as "don't validate this"], + recursiveDenylist: ['myCustomKey' as "don't validate this"], }); expect(console.warn).not.toBeCalled(); diff --git a/packages/jest-validate/src/defaultConfig.ts b/packages/jest-validate/src/defaultConfig.ts index 0b2877702d60..4b932dbccad9 100644 --- a/packages/jest-validate/src/defaultConfig.ts +++ b/packages/jest-validate/src/defaultConfig.ts @@ -22,7 +22,7 @@ const validationOptions: ValidationOptions = { exampleConfig: {}, recursive: true, // Allow NPM-sanctioned comments in package.json. Use a "//" key. - recursiveBlacklist: ['//'], + recursiveDenylist: ['//'], title: { deprecation: DEPRECATION, error: ERROR, diff --git a/packages/jest-validate/src/exampleConfig.ts b/packages/jest-validate/src/exampleConfig.ts index 613cd2b974e2..dac486b2b357 100644 --- a/packages/jest-validate/src/exampleConfig.ts +++ b/packages/jest-validate/src/exampleConfig.ts @@ -17,7 +17,7 @@ const config: ValidationOptions = { error: () => {}, exampleConfig: {key: 'value', test: 'case'}, recursive: true, - recursiveBlacklist: [], + recursiveDenylist: [], title: { deprecation: 'Deprecation Warning', error: 'Validation Error', diff --git a/packages/jest-validate/src/types.ts b/packages/jest-validate/src/types.ts index 879669cc80b7..6e1408a38c19 100644 --- a/packages/jest-validate/src/types.ts +++ b/packages/jest-validate/src/types.ts @@ -33,6 +33,7 @@ export type ValidationOptions = { exampleConfig: Record; recursive?: boolean; recursiveBlacklist?: Array; + recursiveDenylist?: Array; title?: Title; unknown?: ( config: Record, diff --git a/packages/jest-validate/src/validate.ts b/packages/jest-validate/src/validate.ts index 4e63da9eaed9..7a739616a7ec 100644 --- a/packages/jest-validate/src/validate.ts +++ b/packages/jest-validate/src/validate.ts @@ -70,7 +70,11 @@ const _validate = ( options.error(key, config[key], exampleConfig[key], options, path); } } else if ( - shouldSkipValidationForPath(path, key, options.recursiveBlacklist) + shouldSkipValidationForPath( + path, + key, + options.recursiveDenylist || options.recursiveBlacklist, + ) ) { // skip validating unknown options inside blacklisted paths } else { @@ -81,8 +85,12 @@ const _validate = ( if ( options.recursive && !Array.isArray(exampleConfig[key]) && - options.recursiveBlacklist && - !shouldSkipValidationForPath(path, key, options.recursiveBlacklist) + (options.recursiveDenylist || options.recursiveBlacklist) && + !shouldSkipValidationForPath( + path, + key, + options.recursiveDenylist || options.recursiveBlacklist, + ) ) { _validate(config[key], exampleConfig[key], options, [...path, key]); } @@ -101,16 +109,16 @@ const validate = ( ): {hasDeprecationWarnings: boolean; isValid: boolean} => { hasDeprecationWarnings = false; - // Preserve default blacklist entries even with user-supplied blacklist - const combinedBlacklist: Array = [ - ...(defaultConfig.recursiveBlacklist || []), - ...(options.recursiveBlacklist || []), + // Preserve default denylist entries even with user-supplied denylist + const combinedDenylist: Array = [ + ...(defaultConfig.recursiveDenylist || []), + ...(options.recursiveDenylist || options.recursiveBlacklist || []), ]; const defaultedOptions: ValidationOptions = Object.assign({ ...defaultConfig, ...options, - recursiveBlacklist: combinedBlacklist, + recursiveDenylist: combinedDenylist, title: options.title || defaultConfig.title, });