diff --git a/CHANGELOG.md b/CHANGELOG.md index cd5316399f61..94312007da65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-validate]` Add support for `recursiveDenylist` option as an alternative to `recursiveBlacklist` ([#10236](https://github.com/facebook/jest/pull/10236)) + ### Fixes ### Chore & Maintenance diff --git a/packages/jest-validate/README.md b/packages/jest-validate/README.md index ebf791d6075d..f4871ac06cfa 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 1bdf832d6cf9..5275a48bc5df 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 a87171f78cf8..aba81d819edc 100644 --- a/packages/jest-validate/src/types.ts +++ b/packages/jest-validate/src/types.ts @@ -35,6 +35,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 d3a0002812ee..7c4089ba0131 100644 --- a/packages/jest-validate/src/validate.ts +++ b/packages/jest-validate/src/validate.ts @@ -14,8 +14,8 @@ let hasDeprecationWarnings = false; const shouldSkipValidationForPath = ( path: Array, key: string, - blacklist?: Array, -) => (blacklist ? blacklist.includes([...path, key].join('.')) : false); + denylist?: Array, +) => (denylist ? denylist.includes([...path, key].join('.')) : false); const _validate = ( config: Record, @@ -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, });