Skip to content

Commit 577ae0b

Browse files
committed
update tests
1 parent 9bce643 commit 577ae0b

8 files changed

Lines changed: 108 additions & 26 deletions

File tree

packages/jest-config/src/normalize.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ export default function normalize(options: InitialOptions, argv: Argv) {
340340
comment: DOCUMENTATION_NOTE,
341341
deprecatedConfig: DEPRECATED_CONFIG,
342342
exampleConfig: VALID_CONFIG,
343+
recursive: true,
343344
});
344345

345346
options = normalizePreprocessor(

packages/jest-validate/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Where `ValidationOptions` are:
1818

1919
```js
2020
type ValidationOptions = {
21+
blacklist?: Array<string>
2122
comment?: string,
2223
condition?: (option: any, validOption: any) => boolean,
2324
deprecate?: (
@@ -34,6 +35,7 @@ type ValidationOptions = {
3435
options: ValidationOptions,
3536
) => void,
3637
exampleConfig: Object,
38+
recursive?: boolean
3739
title?: Title,
3840
unknown?: (
3941
config: Object,
@@ -60,11 +62,13 @@ Almost anything can be overwritten to suite your needs.
6062

6163
### Options
6264

65+
- `blacklist` – optional array of string keyPaths that should be excluded from deep (recursive) validation.
6366
- `comment` – optional string to be rendered below error/warning message.
6467
- `condition` – an optional function with validation condition.
6568
- `deprecate`, `error`, `unknown` – optional functions responsible for displaying warning and error messages.
6669
- `deprecatedConfig` – optional object with deprecated config keys.
6770
- `exampleConfig` – the only **required** option with configuration against which you'd like to test.
71+
- `recursive` - optional boolean determining whether recursively compare `exampleConfig` to `config`.
6872
- `title` – optional object of titles for errors and messages.
6973

7074
You will find examples of `condition`, `deprecate`, `error`, `unknown`, and `deprecatedConfig` inside source of this repository, named respectively.
@@ -84,6 +88,7 @@ validate(config, {
8488
comment: ' Documentation: http://custom-docs.com',
8589
deprecatedConfig,
8690
exampleConfig,
91+
recursive: true,
8792
title: {
8893
deprecation: 'Custom Deprecation',
8994
// leaving 'error' and 'warning' as default
@@ -116,7 +121,9 @@ This will output:
116121

117122
Example:
118123
{
119-
"transform": {"^.+\\.js$": "<rootDir>/preprocessor.js"}
124+
"transform": {
125+
"^.+\\.js$": "<rootDir>/preprocessor.js"
126+
}
120127
}
121128

122129
Documentation: http://custom-docs.com

packages/jest-validate/src/__tests__/__snapshots__/validate.test.js.snap

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ exports[`pretty prints valid config for Array 1`] = `
3232
<red></>
3333
<red> Example:</>
3434
<red> {</>
35-
<red> <bold>\\"coverageReporters\\"</>: <bold>[\\"json\\", \\"text\\", \\"lcov\\", \\"clover\\"]</></>
35+
<red> <bold>\\"coverageReporters\\"</>: <bold>[</></>
36+
<red><bold> \\"json\\",</></>
37+
<red><bold> \\"text\\",</></>
38+
<red><bold> \\"lcov\\",</></>
39+
<red><bold> \\"clover\\"</></>
40+
<red><bold> ]</></>
3641
<red> }</>
3742
<red></>"
3843
`;
@@ -77,7 +82,12 @@ exports[`pretty prints valid config for Object 1`] = `
7782
<red></>
7883
<red> Example:</>
7984
<red> {</>
80-
<red> <bold>\\"haste\\"</>: <bold>{\\"providesModuleNodeModules\\": [\\"react\\", \\"react-native\\"]}</></>
85+
<red> <bold>\\"haste\\"</>: <bold>{</></>
86+
<red><bold> \\"providesModuleNodeModules\\": [</></>
87+
<red><bold> \\"react\\",</></>
88+
<red><bold> \\"react-native\\"</></>
89+
<red><bold> ]</></>
90+
<red><bold> }</></>
8191
<red> }</>
8292
<red></>"
8393
`;
@@ -122,7 +132,10 @@ exports[`works with custom errors 1`] = `
122132
<red></>
123133
<red> Example:</>
124134
<red> {</>
125-
<red> <bold>\\"test\\"</>: <bold>[1, 2]</></>
135+
<red> <bold>\\"test\\"</>: <bold>[</></>
136+
<red><bold> 1,</></>
137+
<red><bold> 2</></>
138+
<red><bold> ]</></>
126139
<red> }</>
127140
<red></>
128141
<red>My custom comment</>"

packages/jest-validate/src/__tests__/validate.test.js

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,41 @@ const {
1818
deprecatedConfig,
1919
} = require('./fixtures/jest_config');
2020

21-
test('validates default Jest config', () => {
21+
test('recursively validates default Jest config', () => {
2222
expect(
2323
validate(defaultConfig, {
2424
exampleConfig: validConfig,
25+
recursive: true,
2526
}),
2627
).toEqual({
2728
hasDeprecationWarnings: false,
2829
isValid: true,
2930
});
3031
});
3132

32-
test('validates default jest-validate config', () => {
33+
test('recursively validates default jest-validate config', () => {
3334
expect(
3435
validate(jestValidateDefaultConfig, {
3536
exampleConfig: jestValidateExampleConfig,
37+
recursive: true,
3638
}),
3739
).toEqual({
3840
hasDeprecationWarnings: false,
3941
isValid: true,
4042
});
4143
});
4244

43-
[
44-
[{automock: []}, 'Boolean'],
45-
[{coverageReporters: {}}, 'Array'],
46-
[{preset: 1337}, 'String'],
47-
[{haste: 42}, 'Object'],
48-
].forEach(([config, type]) => {
49-
test(`pretty prints valid config for ${type}`, () => {
50-
expect(() =>
51-
validate(config, {
52-
exampleConfig: validConfig,
53-
}),
54-
).toThrowErrorMatchingSnapshot();
55-
});
45+
test.each([
46+
['Boolean', {automock: []}],
47+
['Array', {coverageReporters: {}}],
48+
['String', {preset: 1337}],
49+
['Object', {haste: 42}],
50+
])('pretty prints valid config for %s', (type, config) => {
51+
expect(() =>
52+
validate(config, {
53+
exampleConfig: validConfig,
54+
}),
55+
).toThrowErrorMatchingSnapshot();
5656
});
5757

5858
test(`pretty prints valid config for Function`, () => {
@@ -61,6 +61,7 @@ test(`pretty prints valid config for Function`, () => {
6161
expect(() =>
6262
validate(config, {
6363
exampleConfig: validConfig,
64+
recursive: true,
6465
}),
6566
).toThrowErrorMatchingSnapshot();
6667
});
@@ -76,6 +77,58 @@ test('omits null and undefined config values', () => {
7677
});
7778
});
7879

80+
test('recursively omits null and undefined config values', () => {
81+
const config = {
82+
haste: {
83+
providesModuleNodeModules: null,
84+
},
85+
};
86+
expect(
87+
validate(config, {exampleConfig: validConfig, recursive: true}),
88+
).toEqual({
89+
hasDeprecationWarnings: false,
90+
isValid: true,
91+
});
92+
});
93+
94+
test('respects blacklist', () => {
95+
const warn = console.warn;
96+
console.warn = jest.fn();
97+
const config = {
98+
something: {
99+
nested: {
100+
some_random_key: 'value',
101+
some_random_key2: 'value2',
102+
},
103+
},
104+
};
105+
const exampleConfig = {
106+
something: {
107+
nested: {
108+
test: true,
109+
},
110+
},
111+
};
112+
113+
validate(config, {
114+
exampleConfig,
115+
recursive: true,
116+
});
117+
118+
expect(console.warn).toBeCalled();
119+
120+
console.warn.mockReset();
121+
122+
validate(config, {
123+
blacklist: ['something.nested'],
124+
exampleConfig,
125+
recursive: true,
126+
});
127+
128+
expect(console.warn).not.toBeCalled();
129+
console.warn = warn;
130+
});
131+
79132
test('displays warning for unknown config options', () => {
80133
const config = {unkwon: {}};
81134
const validConfig = {unknown: 'string'};
@@ -97,6 +150,7 @@ test('displays warning for deprecated config options', () => {
97150
validate(config, {
98151
deprecatedConfig,
99152
exampleConfig: validConfig,
153+
recursive: true,
100154
}),
101155
).toEqual({
102156
hasDeprecationWarnings: true,

packages/jest-validate/src/default_config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type {ValidationOptions} from './types';
1212
import {deprecationWarning} from './deprecated';
1313
import {unknownOptionWarning} from './warnings';
1414
import {errorMessage} from './errors';
15-
import exampleConfig from './example_config';
1615
import validationCondition from './condition';
1716
import {ERROR, DEPRECATION, WARNING} from './utils';
1817

@@ -23,7 +22,8 @@ export default ({
2322
deprecate: deprecationWarning,
2423
deprecatedConfig: {},
2524
error: errorMessage,
26-
exampleConfig,
25+
exampleConfig: {},
26+
recursive: false,
2727
title: {
2828
deprecation: DEPRECATION,
2929
error: ERROR,

packages/jest-validate/src/example_config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import type {ValidationOptions} from './types';
1111

1212
const config: ValidationOptions = {
13+
blacklist: [],
1314
comment: ' A comment',
1415
condition: (option, validOption) => true,
1516
deprecate: (config, option, deprecatedOptions, options) => false,
@@ -18,6 +19,7 @@ const config: ValidationOptions = {
1819
},
1920
error: (option, received, defaultValue, options) => {},
2021
exampleConfig: {key: 'value', test: 'case'},
22+
recursive: true,
2123
title: {
2224
deprecation: 'Deprecation Warning',
2325
error: 'Validation Error',

packages/jest-validate/src/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type ValidationOptions = {
3232
path?: Array<string>,
3333
) => void,
3434
exampleConfig: Object,
35+
recursive?: boolean,
3536
title?: Title,
3637
unknown?: (
3738
config: Object,

packages/jest-validate/src/validate.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import defaultConfig from './default_config';
1313

1414
let hasDeprecationWarnings = false;
1515

16+
const shouldSkipValidationForPath = (
17+
path: Array<string>,
18+
key: string,
19+
blacklist: ?Array<string>,
20+
) => (blacklist ? blacklist.includes([...path, key].join('.')) : false);
21+
1622
const _validate = (
1723
config: Object,
1824
exampleConfig: Object,
@@ -50,20 +56,18 @@ const _validate = (
5056
) {
5157
options.error(key, config[key], exampleConfig[key], options, path);
5258
}
53-
} else if (
54-
options.blacklist &&
55-
options.blacklist.includes(path.join('.'))
56-
) {
59+
} else if (shouldSkipValidationForPath(path, key, options.blacklist)) {
5760
// skip validating unknown options inside blacklisted paths
5861
} else {
5962
options.unknown &&
6063
options.unknown(config, exampleConfig, key, options, path);
6164
}
6265

6366
if (
67+
options.recursive &&
6468
!Array.isArray(exampleConfig[key]) &&
6569
options.blacklist &&
66-
!options.blacklist.includes(path.join('.'))
70+
!shouldSkipValidationForPath(path, key, options.blacklist)
6771
) {
6872
_validate(config[key], exampleConfig[key], options, [...path, key]);
6973
}

0 commit comments

Comments
 (0)