forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.test.js
More file actions
216 lines (187 loc) · 4.77 KB
/
Copy pathvalidate.test.js
File metadata and controls
216 lines (187 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
'use strict';
import validate from '../validate';
import jestValidateExampleConfig from '../example_config';
import jestValidateDefaultConfig from '../default_config';
const {
defaultConfig,
validConfig,
deprecatedConfig,
} = require('./fixtures/jest_config');
test('recursively validates default Jest config', () => {
expect(
validate(defaultConfig, {
exampleConfig: validConfig,
recursive: true,
}),
).toEqual({
hasDeprecationWarnings: false,
isValid: true,
});
});
test('recursively validates default jest-validate config', () => {
expect(
validate(jestValidateDefaultConfig, {
exampleConfig: jestValidateExampleConfig,
recursive: true,
}),
).toEqual({
hasDeprecationWarnings: false,
isValid: true,
});
});
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`, () => {
const config = {fn: 'test'};
const validConfig = {fn: (config, option, deprecatedOptions) => true};
expect(() =>
validate(config, {
exampleConfig: validConfig,
recursive: true,
}),
).toThrowErrorMatchingSnapshot();
});
test('omits null and undefined config values', () => {
const config = {
haste: undefined,
preset: null,
};
expect(validate(config, {exampleConfig: validConfig})).toEqual({
hasDeprecationWarnings: false,
isValid: true,
});
});
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'};
const warn = console.warn;
console.warn = jest.fn();
validate(config, {exampleConfig: validConfig});
expect(console.warn.mock.calls[0][0]).toMatchSnapshot();
console.warn = warn;
});
test('displays warning for deprecated config options', () => {
const config = {scriptPreprocessor: 'test'};
const warn = console.warn;
console.warn = jest.fn();
expect(
validate(config, {
deprecatedConfig,
exampleConfig: validConfig,
recursive: true,
}),
).toEqual({
hasDeprecationWarnings: true,
isValid: true,
});
expect(console.warn.mock.calls[0][0]).toMatchSnapshot();
console.warn = warn;
});
test('works with custom warnings', () => {
const config = {unknown: 'string'};
const validConfig = {test: [1, 2]};
const warn = console.warn;
const options = {
comment: 'My custom comment',
deprecatedConfig,
exampleConfig: validConfig,
title: {
warning: 'My Custom Warning',
},
};
console.warn = jest.fn();
validate(config, options);
expect(console.warn.mock.calls[0][0]).toMatchSnapshot();
console.warn = warn;
});
test('works with custom errors', () => {
const config = {test: 'string'};
const validConfig = {test: [1, 2]};
const options = {
comment: 'My custom comment',
deprecatedConfig,
exampleConfig: validConfig,
title: {
error: 'My Custom Error',
},
};
expect(() => validate(config, options)).toThrowErrorMatchingSnapshot();
});
test('works with custom deprecations', () => {
const config = {scriptPreprocessor: 'test'};
const warn = console.warn;
const options = {
comment: 'My custom comment',
deprecatedConfig,
exampleConfig: validConfig,
title: {
deprecation: 'My Custom Deprecation Warning',
},
};
console.warn = jest.fn();
validate(config, options);
expect(console.warn.mock.calls[0][0]).toMatchSnapshot();
console.warn = warn;
});