[assert helpers] Remove toWarnDev from fixtures/dom (#31894)
This is unused and never was: https://github.com/facebook/react/commit/e6a0473c3c6f501dbe291f60b9ee35760ab99eed
Ricky committed
Dec 23, 2024 at 18:11 UTC
97d794958f5b19b66a980f737facd890463f0cb8
2 files changed
-286
fixtures/dom/src/__tests__/nested-act-test.js
-2
@@ -14,8 +14,6 @@ let TestAct;
14
15
global.__DEV__ = process.env.NODE_ENV !== 'production';
16
17
-expect.extend(require('../toWarnDev'));
18
-
17
describe('unmocked scheduler', () => {
18
beforeEach(() => {
19
jest.resetModules();
fixtures/dom/src/toWarnDev.js
deleted
-284
@@ -1,284 +0,0 @@
1
-// copied from scripts/jest/matchers/toWarnDev.js
2
-'use strict';
3
-
4
-const {diff: jestDiff} = require('jest-diff');
5
-const util = require('util');
6
-
7
-function shouldIgnoreConsoleError(format, args) {
8
- if (__DEV__) {
9
- if (typeof format === 'string') {
10
- if (format.indexOf('The above error occurred') === 0) {
11
- // This looks like an error addendum from ReactFiberErrorLogger.
12
- // Ignore it too.
13
- return true;
14
- }
15
- }
16
- } else {
17
- if (
18
- format != null &&
19
- typeof format.message === 'string' &&
20
- typeof format.stack === 'string' &&
21
- args.length === 0
22
- ) {
23
- // In production, ReactFiberErrorLogger logs error objects directly.
24
- // They are noisy too so we'll try to ignore them.
25
- return true;
26
- }
27
- }
28
- // Looks legit
29
- return false;
30
-}
31
-
32
-function normalizeCodeLocInfo(str) {
33
- return str && str.replace(/at .+?:\d+/g, 'at **');
34
-}
35
-
36
-const createMatcherFor = consoleMethod =>
37
- function matcher(callback, expectedMessages, options = {}) {
38
- if (__DEV__) {
39
- // Warn about incorrect usage of matcher.
40
- if (typeof expectedMessages === 'string') {
41
- expectedMessages = [expectedMessages];
42
- } else if (!Array.isArray(expectedMessages)) {
43
- throw Error(
44
- `toWarnDev() requires a parameter of type string or an array of strings ` +
45
- `but was given ${typeof expectedMessages}.`
46
- );
47
- }
48
- if (
49
- options != null &&
50
- (typeof options !== 'object' || Array.isArray(options))
51
- ) {
52
- throw new Error(
53
- 'toWarnDev() second argument, when present, should be an object. ' +
54
- 'Did you forget to wrap the messages into an array?'
55
- );
56
- }
57
- if (arguments.length > 3) {
58
- // `matcher` comes from Jest, so it's more than 2 in practice
59
- throw new Error(
60
- 'toWarnDev() received more than two arguments. ' +
61
- 'Did you forget to wrap the messages into an array?'
62
- );
63
- }
64
-
65
- const withoutStack = options.withoutStack;
66
- const warningsWithoutComponentStack = [];
67
- const warningsWithComponentStack = [];
68
- const unexpectedWarnings = [];
69
-
70
- let lastWarningWithMismatchingFormat = null;
71
- let lastWarningWithExtraComponentStack = null;
72
-
73
- // Catch errors thrown by the callback,
74
- // But only rethrow them if all test expectations have been satisfied.
75
- // Otherwise an Error in the callback can mask a failed expectation,
76
- // and result in a test that passes when it shouldn't.
77
- let caughtError;
78
-
79
- const isLikelyAComponentStack = message =>
80
- typeof message === 'string' && message.includes('\n in ');
81
-
82
- const consoleSpy = (format, ...args) => {
83
- // Ignore uncaught errors reported by jsdom
84
- // and React addendums because they're too noisy.
85
- if (
86
- consoleMethod === 'error' &&
87
- shouldIgnoreConsoleError(format, args)
88
- ) {
89
- return;
90
- }
91
-
92
- const message = util.format(format, ...args);
93
- const normalizedMessage = normalizeCodeLocInfo(message);
94
-
95
- // Remember if the number of %s interpolations
96
- // doesn't match the number of arguments.
97
- // We'll fail the test if it happens.
98
- let argIndex = 0;
99
- format.replace(/%s/g, () => argIndex++);
100
- if (argIndex !== args.length) {
101
- lastWarningWithMismatchingFormat = {
102
- format,
103
- args,
104
- expectedArgCount: argIndex,
105
- };
106
- }
107
-
108
- // Protect against accidentally passing a component stack
109
- // to warning() which already injects the component stack.
110
- if (
111
- args.length >= 2 &&
112
- isLikelyAComponentStack(args[args.length - 1]) &&
113
- isLikelyAComponentStack(args[args.length - 2])
114
- ) {
115
- lastWarningWithExtraComponentStack = {
116
- format,
117
- };
118
- }
119
-
120
- for (let index = 0; index < expectedMessages.length; index++) {
121
- const expectedMessage = expectedMessages[index];
122
- if (
123
- normalizedMessage === expectedMessage ||
124
- normalizedMessage.includes(expectedMessage)
125
- ) {
126
- if (isLikelyAComponentStack(normalizedMessage)) {
127
- warningsWithComponentStack.push(normalizedMessage);
128
- } else {
129
- warningsWithoutComponentStack.push(normalizedMessage);
130
- }
131
- expectedMessages.splice(index, 1);
132
- return;
133
- }
134
- }
135
-
136
- let errorMessage;
137
- if (expectedMessages.length === 0) {
138
- errorMessage =
139
- 'Unexpected warning recorded: ' +
140
- this.utils.printReceived(normalizedMessage);
141
- } else if (expectedMessages.length === 1) {
142
- errorMessage =
143
- 'Unexpected warning recorded: ' +
144
- jestDiff(expectedMessages[0], normalizedMessage);
145
- } else {
146
- errorMessage =
147
- 'Unexpected warning recorded: ' +
148
- jestDiff(expectedMessages, [normalizedMessage]);
149
- }
150
-
151
- // Record the call stack for unexpected warnings.
152
- // We don't throw an Error here though,
153
- // Because it might be suppressed by ReactFiberScheduler.
154
- unexpectedWarnings.push(new Error(errorMessage));
155
- };
156
-
157
- // TODO Decide whether we need to support nested toWarn* expectations.
158
- // If we don't need it, add a check here to see if this is already our spy,
159
- // And throw an error.
160
- const originalMethod = console[consoleMethod];
161
-
162
- // Avoid using Jest's built-in spy since it can't be removed.
163
- console[consoleMethod] = consoleSpy;
164
-
165
- try {
166
- callback();
167
- } catch (error) {
168
- caughtError = error;
169
- } finally {
170
- // Restore the unspied method so that unexpected errors fail tests.
171
- console[consoleMethod] = originalMethod;
172
-
173
- // Any unexpected Errors thrown by the callback should fail the test.
174
- // This should take precedence since unexpected errors could block warnings.
175
- if (caughtError) {
176
- throw caughtError;
177
- }
178
-
179
- // Any unexpected warnings should be treated as a failure.
180
- if (unexpectedWarnings.length > 0) {
181
- return {
182
- message: () => unexpectedWarnings[0].stack,
183
- pass: false,
184
- };
185
- }
186
-
187
- // Any remaining messages indicate a failed expectations.
188
- if (expectedMessages.length > 0) {
189
- return {
190
- message: () =>
191
- `Expected warning was not recorded:\n ${this.utils.printReceived(
192
- expectedMessages[0]
193
- )}`,
194
- pass: false,
195
- };
196
- }
197
-
198
- if (typeof withoutStack === 'number') {
199
- // We're expecting a particular number of warnings without stacks.
200
- if (withoutStack !== warningsWithoutComponentStack.length) {
201
- return {
202
- message: () =>
203
- `Expected ${withoutStack} warnings without a component stack but received ${warningsWithoutComponentStack.length}:\n` +
204
- warningsWithoutComponentStack.map(warning =>
205
- this.utils.printReceived(warning)
206
- ),
207
- pass: false,
208
- };
209
- }
210
- } else if (withoutStack === true) {
211
- // We're expecting that all warnings won't have the stack.
212
- // If some warnings have it, it's an error.
213
- if (warningsWithComponentStack.length > 0) {
214
- return {
215
- message: () =>
216
- `Received warning unexpectedly includes a component stack:\n ${this.utils.printReceived(
217
- warningsWithComponentStack[0]
218
- )}\nIf this warning intentionally includes the component stack, remove ` +
219
- `{withoutStack: true} from the toWarnDev() call. If you have a mix of ` +
220
- `warnings with and without stack in one toWarnDev() call, pass ` +
221
- `{withoutStack: N} where N is the number of warnings without stacks.`,
222
- pass: false,
223
- };
224
- }
225
- } else if (withoutStack === false || withoutStack === undefined) {
226
- // We're expecting that all warnings *do* have the stack (default).
227
- // If some warnings don't have it, it's an error.
228
- if (warningsWithoutComponentStack.length > 0) {
229
- return {
230
- message: () =>
231
- `Received warning unexpectedly does not include a component stack:\n ${this.utils.printReceived(
232
- warningsWithoutComponentStack[0]
233
- )}\nIf this warning intentionally omits the component stack, add ` +
234
- `{withoutStack: true} to the toWarnDev() call.`,
235
- pass: false,
236
- };
237
- }
238
- } else {
239
- throw Error(
240
- `The second argument for toWarnDev(), when specified, must be an object. It may have a ` +
241
- `property called "withoutStack" whose value may be undefined, boolean, or a number. ` +
242
- `Instead received ${typeof withoutStack}.`
243
- );
244
- }
245
-
246
- if (lastWarningWithMismatchingFormat !== null) {
247
- return {
248
- message: () =>
249
- `Received ${
250
- lastWarningWithMismatchingFormat.args.length
251
- } arguments for a message with ${
252
- lastWarningWithMismatchingFormat.expectedArgCount
253
- } placeholders:\n ${this.utils.printReceived(
254
- lastWarningWithMismatchingFormat.format
255
- )}`,
256
- pass: false,
257
- };
258
- }
259
-
260
- if (lastWarningWithExtraComponentStack !== null) {
261
- return {
262
- message: () =>
263
- `Received more than one component stack for a warning:\n ${this.utils.printReceived(
264
- lastWarningWithExtraComponentStack.format
265
- )}\nDid you accidentally pass a stack to warning() as the last argument? ` +
266
- `Don't forget warning() already injects the component stack automatically.`,
267
- pass: false,
268
- };
269
- }
270
-
271
- return {pass: true};
272
- }
273
- } else {
274
- // Any uncaught errors or warnings should fail tests in production mode.
275
- callback();
276
-
277
- return {pass: true};
278
- }
279
- };
280
-
281
-module.exports = {
282
- toLowPriorityWarnDev: createMatcherFor('warn'),
283
- toWarnDev: createMatcherFor('error'),
284
-};