mirror of
https://github.com/facebook/react.git
synced 2026-02-25 13:13:03 +00:00
Stacked on top of #28498 for test fixes. ### Don't Rethrow When we started React it was 1:1 setState calls a series of renders and if they error, it errors where the setState was called. Simple. However, then batching came and the error actually got thrown somewhere else. With concurrent mode, it's not even possible to get setState itself to throw anymore. In fact, all APIs that can rethrow out of React are executed either at the root of the scheduler or inside a DOM event handler. If you throw inside a React.startTransition callback that's sync, then that will bubble out of the startTransition but if you throw inside an async callback or a useTransition we now need to handle it at the hook site. So in 19 we need to make all React.startTransition swallow the error (and report them to reportError). The only one remaining that can throw is flushSync but it doesn't really make sense for it to throw at the callsite neither because batching. Just because something rendered in this flush doesn't mean it was rendered due to what was just scheduled and doesn't mean that it should abort any of the remaining code afterwards. setState is fire and forget. It's send an instruction elsewhere, it's not part of the current imperative code. Error boundaries never rethrow. Since you should really always have error boundaries, most of the time, it wouldn't rethrow anyway. Rethrowing also actually currently drops errors on the floor since we can only rethrow the first error, so to avoid that we'd need to call reportError anyway. This happens in RN events. The other issue with rethrowing is that it logs an extra console.error. Since we're not sure that user code will actually log it anywhere we still log it too just like we do with errors inside error boundaries which leads all of these to log twice. The goal of this PR is to never rethrow out of React instead, errors outside of error boundaries get logged to reportError. Event system errors too. ### Breaking Changes The main thing this affects is testing where you want to inspect the errors thrown. To make it easier to port, if you're inside `act` we track the error into act in an aggregate error and then rethrow it at the root of `act`. Unlike before though, if you flush synchronously inside of act it'll still continue until the end of act before rethrowing. I expect most user code breakages would be to migrate from `flushSync` to `act` if you assert on throwing. However, in the React repo we also have `internalAct` and the `waitForThrow` helpers. Since these have to use public production implementations we track these using the global onerror or process uncaughtException. Unlike regular act, includes both event handler errors and onRecoverableError by default too. Not just render/commit errors. So I had to account for that in our tests. We restore logging an extra log for uncaught errors after the main log with the component stack in it. We use `console.warn`. This is not yet ignorable if you preventDefault to the main error event. To avoid confusion if you don't end up logging the error to console I just added `An error occurred`. ### Polyfill All browsers we support really supports `reportError` but not all test and server environments do, so I implemented a polyfill for browser and node in `shared/reportGlobalError`. I don't love that this is included in all builds and gets duplicated into isomorphic even though it's not actually needed in production. Maybe in the future we can require a polyfill for this. ### Follow Ups In a follow up, I'll make caught vs uncaught error handling be configurable too. --------- Co-authored-by: Ricky Hanlon <rickhanlonii@gmail.com>
356 lines
10 KiB
JavaScript
356 lines
10 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import * as SchedulerMock from 'scheduler/unstable_mock';
|
|
import {diff} from 'jest-diff';
|
|
import {equals} from '@jest/expect-utils';
|
|
import enqueueTask from './enqueueTask';
|
|
import simulateBrowserEventDispatch from './simulateBrowserEventDispatch';
|
|
|
|
export {act} from './internalAct';
|
|
|
|
import {thrownErrors, actingUpdatesScopeDepth} from './internalAct';
|
|
|
|
function assertYieldsWereCleared(caller) {
|
|
const actualYields = SchedulerMock.unstable_clearLog();
|
|
if (actualYields.length !== 0) {
|
|
const error = Error(
|
|
'The event log is not empty. Call assertLog(...) first.',
|
|
);
|
|
Error.captureStackTrace(error, caller);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function waitForMicrotasks() {
|
|
return new Promise(resolve => {
|
|
enqueueTask(() => resolve());
|
|
});
|
|
}
|
|
|
|
export async function waitFor(expectedLog, options) {
|
|
assertYieldsWereCleared(waitFor);
|
|
|
|
// Create the error object before doing any async work, to get a better
|
|
// stack trace.
|
|
const error = new Error();
|
|
Error.captureStackTrace(error, waitFor);
|
|
|
|
const stopAfter = expectedLog.length;
|
|
const actualLog = [];
|
|
do {
|
|
// Wait until end of current task/microtask.
|
|
await waitForMicrotasks();
|
|
if (SchedulerMock.unstable_hasPendingWork()) {
|
|
SchedulerMock.unstable_flushNumberOfYields(stopAfter - actualLog.length);
|
|
actualLog.push(...SchedulerMock.unstable_clearLog());
|
|
if (stopAfter > actualLog.length) {
|
|
// Continue flushing until we've logged the expected number of items.
|
|
} else {
|
|
// Once we've reached the expected sequence, wait one more microtask to
|
|
// flush any remaining synchronous work.
|
|
await waitForMicrotasks();
|
|
actualLog.push(...SchedulerMock.unstable_clearLog());
|
|
break;
|
|
}
|
|
} else {
|
|
// There's no pending work, even after a microtask.
|
|
break;
|
|
}
|
|
} while (true);
|
|
|
|
if (options && options.additionalLogsAfterAttemptingToYield) {
|
|
expectedLog = expectedLog.concat(
|
|
options.additionalLogsAfterAttemptingToYield,
|
|
);
|
|
}
|
|
|
|
if (equals(actualLog, expectedLog)) {
|
|
return;
|
|
}
|
|
|
|
error.message = `
|
|
Expected sequence of events did not occur.
|
|
|
|
${diff(expectedLog, actualLog)}
|
|
`;
|
|
throw error;
|
|
}
|
|
|
|
export async function waitForAll(expectedLog) {
|
|
assertYieldsWereCleared(waitForAll);
|
|
|
|
// Create the error object before doing any async work, to get a better
|
|
// stack trace.
|
|
const error = new Error();
|
|
Error.captureStackTrace(error, waitForAll);
|
|
|
|
do {
|
|
// Wait until end of current task/microtask.
|
|
await waitForMicrotasks();
|
|
if (!SchedulerMock.unstable_hasPendingWork()) {
|
|
// There's no pending work, even after a microtask. Stop flushing.
|
|
break;
|
|
}
|
|
SchedulerMock.unstable_flushAllWithoutAsserting();
|
|
} while (true);
|
|
|
|
const actualLog = SchedulerMock.unstable_clearLog();
|
|
if (equals(actualLog, expectedLog)) {
|
|
return;
|
|
}
|
|
|
|
error.message = `
|
|
Expected sequence of events did not occur.
|
|
|
|
${diff(expectedLog, actualLog)}
|
|
`;
|
|
throw error;
|
|
}
|
|
|
|
function aggregateErrors(errors: Array<mixed>): mixed {
|
|
if (errors.length > 1 && typeof AggregateError === 'function') {
|
|
// eslint-disable-next-line no-undef
|
|
return new AggregateError(errors);
|
|
}
|
|
return errors[0];
|
|
}
|
|
|
|
export async function waitForThrow(expectedError: mixed): mixed {
|
|
assertYieldsWereCleared(waitForThrow);
|
|
|
|
// Create the error object before doing any async work, to get a better
|
|
// stack trace.
|
|
const error = new Error();
|
|
Error.captureStackTrace(error, waitForThrow);
|
|
|
|
do {
|
|
// Wait until end of current task/microtask.
|
|
await waitForMicrotasks();
|
|
if (!SchedulerMock.unstable_hasPendingWork()) {
|
|
// There's no pending work, even after a microtask. Stop flushing.
|
|
error.message = 'Expected something to throw, but nothing did.';
|
|
throw error;
|
|
}
|
|
|
|
const errorHandlerDOM = function (event: ErrorEvent) {
|
|
// Prevent logs from reprinting this error.
|
|
event.preventDefault();
|
|
thrownErrors.push(event.error);
|
|
};
|
|
const errorHandlerNode = function (err: mixed) {
|
|
thrownErrors.push(err);
|
|
};
|
|
// We track errors that were logged globally as if they occurred in this scope and then rethrow them.
|
|
if (actingUpdatesScopeDepth === 0) {
|
|
if (
|
|
typeof window === 'object' &&
|
|
typeof window.addEventListener === 'function'
|
|
) {
|
|
// We're in a JS DOM environment.
|
|
window.addEventListener('error', errorHandlerDOM);
|
|
} else if (typeof process === 'object') {
|
|
// Node environment
|
|
process.on('uncaughtException', errorHandlerNode);
|
|
}
|
|
}
|
|
try {
|
|
SchedulerMock.unstable_flushAllWithoutAsserting();
|
|
} catch (x) {
|
|
thrownErrors.push(x);
|
|
} finally {
|
|
if (actingUpdatesScopeDepth === 0) {
|
|
if (
|
|
typeof window === 'object' &&
|
|
typeof window.addEventListener === 'function'
|
|
) {
|
|
// We're in a JS DOM environment.
|
|
window.removeEventListener('error', errorHandlerDOM);
|
|
} else if (typeof process === 'object') {
|
|
// Node environment
|
|
process.off('uncaughtException', errorHandlerNode);
|
|
}
|
|
}
|
|
}
|
|
if (thrownErrors.length > 0) {
|
|
const thrownError = aggregateErrors(thrownErrors);
|
|
thrownErrors.length = 0;
|
|
|
|
if (expectedError === undefined) {
|
|
// If no expected error was provided, then assume the caller is OK with
|
|
// any error being thrown. We're returning the error so they can do
|
|
// their own checks, if they wish.
|
|
return thrownError;
|
|
}
|
|
if (equals(thrownError, expectedError)) {
|
|
return thrownError;
|
|
}
|
|
if (
|
|
typeof expectedError === 'string' &&
|
|
typeof thrownError === 'object' &&
|
|
thrownError !== null &&
|
|
typeof thrownError.message === 'string'
|
|
) {
|
|
if (thrownError.message.includes(expectedError)) {
|
|
return thrownError;
|
|
} else {
|
|
error.message = `
|
|
Expected error was not thrown.
|
|
|
|
${diff(expectedError, thrownError.message)}
|
|
`;
|
|
throw error;
|
|
}
|
|
}
|
|
error.message = `
|
|
Expected error was not thrown.
|
|
|
|
${diff(expectedError, thrownError)}
|
|
`;
|
|
throw error;
|
|
}
|
|
} while (true);
|
|
}
|
|
|
|
// This is prefixed with `unstable_` because you should almost always try to
|
|
// avoid using it in tests. It's really only for testing a particular
|
|
// implementation detail (update starvation prevention).
|
|
export async function unstable_waitForExpired(expectedLog): mixed {
|
|
assertYieldsWereCleared(unstable_waitForExpired);
|
|
|
|
// Create the error object before doing any async work, to get a better
|
|
// stack trace.
|
|
const error = new Error();
|
|
Error.captureStackTrace(error, unstable_waitForExpired);
|
|
|
|
// Wait until end of current task/microtask.
|
|
await waitForMicrotasks();
|
|
SchedulerMock.unstable_flushExpired();
|
|
|
|
const actualLog = SchedulerMock.unstable_clearLog();
|
|
if (equals(actualLog, expectedLog)) {
|
|
return;
|
|
}
|
|
|
|
error.message = `
|
|
Expected sequence of events did not occur.
|
|
|
|
${diff(expectedLog, actualLog)}
|
|
`;
|
|
throw error;
|
|
}
|
|
|
|
// TODO: This name is a bit misleading currently because it will stop as soon as
|
|
// React yields for any reason, not just for a paint. I've left it this way for
|
|
// now because that's how untable_flushUntilNextPaint already worked, but maybe
|
|
// we should split these use cases into separate APIs.
|
|
export async function waitForPaint(expectedLog) {
|
|
assertYieldsWereCleared(waitForPaint);
|
|
|
|
// Create the error object before doing any async work, to get a better
|
|
// stack trace.
|
|
const error = new Error();
|
|
Error.captureStackTrace(error, waitForPaint);
|
|
|
|
// Wait until end of current task/microtask.
|
|
await waitForMicrotasks();
|
|
if (SchedulerMock.unstable_hasPendingWork()) {
|
|
// Flush until React yields.
|
|
SchedulerMock.unstable_flushUntilNextPaint();
|
|
// Wait one more microtask to flush any remaining synchronous work.
|
|
await waitForMicrotasks();
|
|
}
|
|
|
|
const actualLog = SchedulerMock.unstable_clearLog();
|
|
if (equals(actualLog, expectedLog)) {
|
|
return;
|
|
}
|
|
|
|
error.message = `
|
|
Expected sequence of events did not occur.
|
|
|
|
${diff(expectedLog, actualLog)}
|
|
`;
|
|
throw error;
|
|
}
|
|
|
|
export async function waitForDiscrete(expectedLog) {
|
|
assertYieldsWereCleared(waitForDiscrete);
|
|
|
|
// Create the error object before doing any async work, to get a better
|
|
// stack trace.
|
|
const error = new Error();
|
|
Error.captureStackTrace(error, waitForDiscrete);
|
|
|
|
// Wait until end of current task/microtask.
|
|
await waitForMicrotasks();
|
|
|
|
const actualLog = SchedulerMock.unstable_clearLog();
|
|
if (equals(actualLog, expectedLog)) {
|
|
return;
|
|
}
|
|
|
|
error.message = `
|
|
Expected sequence of events did not occur.
|
|
|
|
${diff(expectedLog, actualLog)}
|
|
`;
|
|
throw error;
|
|
}
|
|
|
|
export function assertLog(expectedLog) {
|
|
const actualLog = SchedulerMock.unstable_clearLog();
|
|
if (equals(actualLog, expectedLog)) {
|
|
return;
|
|
}
|
|
|
|
const error = new Error(`
|
|
Expected sequence of events did not occur.
|
|
|
|
${diff(expectedLog, actualLog)}
|
|
`);
|
|
Error.captureStackTrace(error, assertLog);
|
|
throw error;
|
|
}
|
|
|
|
// Simulates dispatching events, waiting for microtasks in between.
|
|
// This matches the browser behavior, which will flush microtasks
|
|
// between each event handler. This will allow discrete events to
|
|
// flush between events across different event handlers.
|
|
export async function simulateEventDispatch(
|
|
node: Node,
|
|
eventType: string,
|
|
): Promise<void> {
|
|
// Ensure the node is in the document.
|
|
for (let current = node; current; current = current.parentNode) {
|
|
if (current === document) {
|
|
break;
|
|
} else if (current.parentNode == null) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
const customEvent = new Event(eventType, {
|
|
bubbles: true,
|
|
});
|
|
|
|
Object.defineProperty(customEvent, 'target', {
|
|
// Override the target to the node on which we dispatched the event.
|
|
value: node,
|
|
});
|
|
|
|
const impl = Object.getOwnPropertySymbols(node)[0];
|
|
const oldDispatch = node[impl].dispatchEvent;
|
|
try {
|
|
node[impl].dispatchEvent = simulateBrowserEventDispatch;
|
|
|
|
await node.dispatchEvent(customEvent);
|
|
} finally {
|
|
node[impl].dispatchEvent = oldDispatch;
|
|
}
|
|
}
|