Improve stack trace when gated test fails (#28374)
Sebastian Silbermann committed
Feb 18, 2024 at 18:17 UTC
59831c98cffe0edf706238b067928e7cf54d1065
1 file changed
+9
-5
scripts/jest/setupTests.js
+9
-5
@@ -241,7 +241,7 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
241
global.Error = ErrorProxy;
242
}
243
244
- const expectTestToFail = async (callback, errorMsg) => {
244
+ const expectTestToFail = async (callback, error) => {
245
if (callback.length > 0) {
246
throw Error(
247
'Gated test helpers do not support the `done` callback. Return a ' +
@@ -261,12 +261,12 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
261
// `afterEach` like we normally do. `afterEach` is too late because if it
262
// throws, we won't have captured it.
263
flushAllUnexpectedConsoleCalls();
264
- } catch (error) {
264
+ } catch (testError) {
265
// Failed as expected
266
resetAllUnexpectedConsoleCalls();
267
return;
268
}
269
- throw Error(errorMsg);
269
+ throw error;
270
};
271
272
const gatedErrorMessage = 'Gated test was expected to fail, but it passed.';
@@ -284,8 +284,10 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
284
if (shouldPass) {
285
test(testName, callback);
286
} else {
287
+ const error = new Error(gatedErrorMessage);
288
+ Error.captureStackTrace(error, global._test_gate);
289
test(`[GATED, SHOULD FAIL] ${testName}`, () =>
288
- expectTestToFail(callback, gatedErrorMessage));
290
+ expectTestToFail(callback, error));
291
}
292
};
293
global._test_gate_focus = (gateFn, testName, callback) => {
@@ -302,8 +304,10 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
304
if (shouldPass) {
305
test.only(testName, callback);
306
} else {
307
+ const error = new Error(gatedErrorMessage);
308
+ Error.captureStackTrace(error, global._test_gate_focus);
309
test.only(`[GATED, SHOULD FAIL] ${testName}`, () =>
306
- expectTestToFail(callback, gatedErrorMessage));
310
+ expectTestToFail(callback, error));
311
}
312
};
313