Fail tests if toWarnDev() does not wrap warnings in array (#13227)
* Fail tests if toWarn() does not wrap warnings in array * Fix newly failing tests * Another fix
Dan Abramov committed
Jul 18, 2018 at 02:38 UTC
236f6087236c42ccafe94674cd1c5736a1868b00
5 files changed
+73
-10
packages/react-dom/src/__tests__/ReactChildReconciler-test.js
+4
-4
@@ -80,8 +80,8 @@ describe('ReactChildReconciler', () => {
80
'Keys should be unique so that components maintain their identity ' +
81
'across updates. Non-unique keys may cause children to be ' +
82
'duplicated and/or omitted — the behavior is unsupported and ' +
83
- 'could change in a future version.',
84
- ' in div (at **)\n' +
83
+ 'could change in a future version.\n' +
84
+ ' in div (at **)\n' +
85
' in Component (at **)\n' +
86
' in Parent (at **)\n' +
87
' in GrandParent (at **)',
@@ -127,8 +127,8 @@ describe('ReactChildReconciler', () => {
127
'Keys should be unique so that components maintain their identity ' +
128
'across updates. Non-unique keys may cause children to be ' +
129
'duplicated and/or omitted — the behavior is unsupported and ' +
130
- 'could change in a future version.',
131
- ' in div (at **)\n' +
130
+ 'could change in a future version.\n' +
131
+ ' in div (at **)\n' +
132
' in Component (at **)\n' +
133
' in Parent (at **)\n' +
134
' in GrandParent (at **)',
packages/react-dom/src/__tests__/ReactDOMFiber-test.js
+3
-2
@@ -980,8 +980,9 @@ describe('ReactDOMFiber', () => {
980
expect(() => ReactDOM.render(<Example />, container)).toWarnDev(
981
'Expected `onClick` listener to be a function, instead got `false`.\n\n' +
982
'If you used to conditionally omit it with onClick={condition && value}, ' +
983
- 'pass onClick={condition ? value : undefined} instead.\n',
984
- ' in div (at **)\n' + ' in Example (at **)',
983
+ 'pass onClick={condition ? value : undefined} instead.\n' +
984
+ ' in div (at **)\n' +
985
+ ' in Example (at **)',
986
);
987
});
988
packages/react-dom/src/__tests__/ReactMultiChild-test.js
+4
-4
@@ -209,8 +209,8 @@ describe('ReactMultiChild', () => {
209
'Keys should be unique so that components maintain their identity ' +
210
'across updates. Non-unique keys may cause children to be ' +
211
'duplicated and/or omitted — the behavior is unsupported and ' +
212
- 'could change in a future version.',
213
- ' in div (at **)\n' +
212
+ 'could change in a future version.\n' +
213
+ ' in div (at **)\n' +
214
' in WrapperComponent (at **)\n' +
215
' in div (at **)\n' +
216
' in Parent (at **)',
@@ -269,8 +269,8 @@ describe('ReactMultiChild', () => {
269
'Keys should be unique so that components maintain their identity ' +
270
'across updates. Non-unique keys may cause children to be ' +
271
'duplicated and/or omitted — the behavior is unsupported and ' +
272
- 'could change in a future version.',
273
- ' in div (at **)\n' +
272
+ 'could change in a future version.\n' +
273
+ ' in div (at **)\n' +
274
' in WrapperComponent (at **)\n' +
275
' in div (at **)\n' +
276
' in Parent (at **)',
scripts/jest/matchers/__tests__/toWarnDev-test.js
+46
@@ -169,5 +169,51 @@ describe('toWarnDev', () => {
169
}).toWarnDev('Hi');
170
}).toThrow('Received more than one component stack for a warning');
171
});
172
+
173
+ it('fails if multiple strings are passed without an array wrapper', () => {
174
+ expect(() => {
175
+ expect(() => {
176
+ console.error('Hi \n in div');
177
+ }).toWarnDev('Hi', 'Bye');
178
+ }).toThrow(
179
+ 'toWarnDev() second argument, when present, should be an object'
180
+ );
181
+ expect(() => {
182
+ expect(() => {
183
+ console.error('Hi \n in div');
184
+ console.error('Bye \n in div');
185
+ }).toWarnDev('Hi', 'Bye');
186
+ }).toThrow(
187
+ 'toWarnDev() second argument, when present, should be an object'
188
+ );
189
+ expect(() => {
190
+ expect(() => {
191
+ console.error('Hi \n in div');
192
+ console.error('Wow \n in div');
193
+ console.error('Bye \n in div');
194
+ }).toWarnDev('Hi', 'Bye');
195
+ }).toThrow(
196
+ 'toWarnDev() second argument, when present, should be an object'
197
+ );
198
+ expect(() => {
199
+ expect(() => {
200
+ console.error('Hi \n in div');
201
+ console.error('Wow \n in div');
202
+ console.error('Bye \n in div');
203
+ }).toWarnDev('Hi', 'Wow', 'Bye');
204
+ }).toThrow(
205
+ 'toWarnDev() second argument, when present, should be an object'
206
+ );
207
+ });
208
+
209
+ it('fails on more than two arguments', () => {
210
+ expect(() => {
211
+ expect(() => {
212
+ console.error('Hi \n in div');
213
+ console.error('Wow \n in div');
214
+ console.error('Bye \n in div');
215
+ }).toWarnDev('Hi', undefined, 'Bye');
216
+ }).toThrow('toWarnDev() received more than two arguments.');
217
+ });
218
}
219
});
scripts/jest/matchers/toWarnDev.js
+16
@@ -19,6 +19,22 @@ const createMatcherFor = consoleMethod =>
19
`but was given ${typeof expectedMessages}.`
20
);
21
}
22
+ if (
23
+ options != null &&
24
+ (typeof options !== 'object' || Array.isArray(options))
25
+ ) {
26
+ throw new Error(
27
+ 'toWarnDev() second argument, when present, should be an object. ' +
28
+ 'Did you forget to wrap the messages into an array?'
29
+ );
30
+ }
31
+ if (arguments.length > 3) {
32
+ // `matcher` comes from Jest, so it's more than 2 in practice
33
+ throw new Error(
34
+ 'toWarnDev() received more than two arguments. ' +
35
+ 'Did you forget to wrap the messages into an array?'
36
+ );
37
+ }
38
39
const withoutStack = options.withoutStack;
40
const warningsWithoutComponentStack = [];