Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee committed Aug 3, 2018
1 parent 9bce643 commit 577ae0b
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ export default function normalize(options: InitialOptions, argv: Argv) {
comment: DOCUMENTATION_NOTE,
deprecatedConfig: DEPRECATED_CONFIG,
exampleConfig: VALID_CONFIG,
recursive: true,
});

options = normalizePreprocessor(
Expand Down
9 changes: 8 additions & 1 deletion packages/jest-validate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Where `ValidationOptions` are:

```js
type ValidationOptions = {
blacklist?: Array<string>
comment?: string,
condition?: (option: any, validOption: any) => boolean,
deprecate?: (
Expand All @@ -34,6 +35,7 @@ type ValidationOptions = {
options: ValidationOptions,
) => void,
exampleConfig: Object,
recursive?: boolean
title?: Title,
unknown?: (
config: Object,
Expand All @@ -60,11 +62,13 @@ Almost anything can be overwritten to suite your needs.

### Options

- `blacklist` – 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.
- `deprecatedConfig` – optional object with deprecated config keys.
- `exampleConfig` – the only **required** option with configuration against which you'd like to test.
- `recursive` - optional boolean determining whether recursively compare `exampleConfig` to `config`.
- `title` – optional object of titles for errors and messages.

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

Example:
{
"transform": {"^.+\\.js$": "<rootDir>/preprocessor.js"}
"transform": {
"^.+\\.js$": "<rootDir>/preprocessor.js"
}
}

Documentation: http://custom-docs.com
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ exports[`pretty prints valid config for Array 1`] = `
<red></>
<red> Example:</>
<red> {</>
<red> <bold>\\"coverageReporters\\"</>: <bold>[\\"json\\", \\"text\\", \\"lcov\\", \\"clover\\"]</></>
<red> <bold>\\"coverageReporters\\"</>: <bold>[</></>
<red><bold> \\"json\\",</></>
<red><bold> \\"text\\",</></>
<red><bold> \\"lcov\\",</></>
<red><bold> \\"clover\\"</></>
<red><bold> ]</></>
<red> }</>
<red></>"
`;
Expand Down Expand Up @@ -77,7 +82,12 @@ exports[`pretty prints valid config for Object 1`] = `
<red></>
<red> Example:</>
<red> {</>
<red> <bold>\\"haste\\"</>: <bold>{\\"providesModuleNodeModules\\": [\\"react\\", \\"react-native\\"]}</></>
<red> <bold>\\"haste\\"</>: <bold>{</></>
<red><bold> \\"providesModuleNodeModules\\": [</></>
<red><bold> \\"react\\",</></>
<red><bold> \\"react-native\\"</></>
<red><bold> ]</></>
<red><bold> }</></>
<red> }</>
<red></>"
`;
Expand Down Expand Up @@ -122,7 +132,10 @@ exports[`works with custom errors 1`] = `
<red></>
<red> Example:</>
<red> {</>
<red> <bold>\\"test\\"</>: <bold>[1, 2]</></>
<red> <bold>\\"test\\"</>: <bold>[</></>
<red><bold> 1,</></>
<red><bold> 2</></>
<red><bold> ]</></>
<red> }</>
<red></>
<red>My custom comment</>"
Expand Down
84 changes: 69 additions & 15 deletions packages/jest-validate/src/__tests__/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,41 @@ const {
deprecatedConfig,
} = require('./fixtures/jest_config');

test('validates default Jest config', () => {
test('recursively validates default Jest config', () => {
expect(
validate(defaultConfig, {
exampleConfig: validConfig,
recursive: true,
}),
).toEqual({
hasDeprecationWarnings: false,
isValid: true,
});
});

test('validates default jest-validate config', () => {
test('recursively validates default jest-validate config', () => {
expect(
validate(jestValidateDefaultConfig, {
exampleConfig: jestValidateExampleConfig,
recursive: true,
}),
).toEqual({
hasDeprecationWarnings: false,
isValid: true,
});
});

[
[{automock: []}, 'Boolean'],
[{coverageReporters: {}}, 'Array'],
[{preset: 1337}, 'String'],
[{haste: 42}, 'Object'],
].forEach(([config, type]) => {
test(`pretty prints valid config for ${type}`, () => {
expect(() =>
validate(config, {
exampleConfig: validConfig,
}),
).toThrowErrorMatchingSnapshot();
});
test.each([
['Boolean', {automock: []}],
['Array', {coverageReporters: {}}],
['String', {preset: 1337}],
['Object', {haste: 42}],
])('pretty prints valid config for %s', (type, config) => {
expect(() =>
validate(config, {
exampleConfig: validConfig,
}),
).toThrowErrorMatchingSnapshot();
});

test(`pretty prints valid config for Function`, () => {
Expand All @@ -61,6 +61,7 @@ test(`pretty prints valid config for Function`, () => {
expect(() =>
validate(config, {
exampleConfig: validConfig,
recursive: true,
}),
).toThrowErrorMatchingSnapshot();
});
Expand All @@ -76,6 +77,58 @@ test('omits null and undefined config values', () => {
});
});

test('recursively omits null and undefined config values', () => {
const config = {
haste: {
providesModuleNodeModules: null,
},
};
expect(
validate(config, {exampleConfig: validConfig, recursive: true}),
).toEqual({
hasDeprecationWarnings: false,
isValid: true,
});
});

test('respects blacklist', () => {
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,
recursive: true,
});

expect(console.warn).toBeCalled();

console.warn.mockReset();

validate(config, {
blacklist: ['something.nested'],
exampleConfig,
recursive: true,
});

expect(console.warn).not.toBeCalled();
console.warn = warn;
});

test('displays warning for unknown config options', () => {
const config = {unkwon: {}};
const validConfig = {unknown: 'string'};
Expand All @@ -97,6 +150,7 @@ test('displays warning for deprecated config options', () => {
validate(config, {
deprecatedConfig,
exampleConfig: validConfig,
recursive: true,
}),
).toEqual({
hasDeprecationWarnings: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-validate/src/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {ValidationOptions} from './types';
import {deprecationWarning} from './deprecated';
import {unknownOptionWarning} from './warnings';
import {errorMessage} from './errors';
import exampleConfig from './example_config';
import validationCondition from './condition';
import {ERROR, DEPRECATION, WARNING} from './utils';

Expand All @@ -23,7 +22,8 @@ export default ({
deprecate: deprecationWarning,
deprecatedConfig: {},
error: errorMessage,
exampleConfig,
exampleConfig: {},
recursive: false,
title: {
deprecation: DEPRECATION,
error: ERROR,
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-validate/src/example_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type {ValidationOptions} from './types';

const config: ValidationOptions = {
blacklist: [],
comment: ' A comment',
condition: (option, validOption) => true,
deprecate: (config, option, deprecatedOptions, options) => false,
Expand All @@ -18,6 +19,7 @@ const config: ValidationOptions = {
},
error: (option, received, defaultValue, options) => {},
exampleConfig: {key: 'value', test: 'case'},
recursive: true,
title: {
deprecation: 'Deprecation Warning',
error: 'Validation Error',
Expand Down
1 change: 1 addition & 0 deletions packages/jest-validate/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type ValidationOptions = {
path?: Array<string>,
) => void,
exampleConfig: Object,
recursive?: boolean,
title?: Title,
unknown?: (
config: Object,
Expand Down
14 changes: 9 additions & 5 deletions packages/jest-validate/src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import defaultConfig from './default_config';

let hasDeprecationWarnings = false;

const shouldSkipValidationForPath = (
path: Array<string>,
key: string,
blacklist: ?Array<string>,
) => (blacklist ? blacklist.includes([...path, key].join('.')) : false);

const _validate = (
config: Object,
exampleConfig: Object,
Expand Down Expand Up @@ -50,20 +56,18 @@ const _validate = (
) {
options.error(key, config[key], exampleConfig[key], options, path);
}
} else if (
options.blacklist &&
options.blacklist.includes(path.join('.'))
) {
} else if (shouldSkipValidationForPath(path, key, options.blacklist)) {
// skip validating unknown options inside blacklisted paths
} else {
options.unknown &&
options.unknown(config, exampleConfig, key, options, path);
}

if (
options.recursive &&
!Array.isArray(exampleConfig[key]) &&
options.blacklist &&
!options.blacklist.includes(path.join('.'))
!shouldSkipValidationForPath(path, key, options.blacklist)
) {
_validate(config[key], exampleConfig[key], options, [...path, key]);
}
Expand Down

0 comments on commit 577ae0b

Please sign in to comment.