Skip to content

Commit

Permalink
Update gate pragma to detect global error events (#28591)
Browse files Browse the repository at this point in the history
If a global error event is dispatched during a test, Jest reports that
test as a failure.

Our `@gate` pragma feature should account for this — if the gate
condition is false, and the global error event is dispatched, then the
test should be reported as a success.

The solution is to install an error event handler right before invoking
the test function. Because we install our own handler, Jest will not
report the test as a failure if a global error event is dispatched; it's
conceptually as if we wrapped the whole test event in a try-catch.
  • Loading branch information
acdlite authored Mar 20, 2024
1 parent 82c6595 commit 29a6ca3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
9 changes: 9 additions & 0 deletions scripts/babel/__tests__/transform-test-gate-pragma-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ describe('transform test-gate-pragma: actual runtime', () => {
console.error('Stop that!');
throw Error('I told you to stop!');
});

// @gate false
test('a global error event is treated as a test failure', () => {
dispatchEvent(
new ErrorEvent('error', {
error: new Error('Oops!'),
})
);
});
});

describe('dynamic gate method', () => {
Expand Down
36 changes: 31 additions & 5 deletions scripts/jest/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,30 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
global.Error = ErrorProxy;
}

const expectTestToFail = async (callback, error) => {
const expectTestToFail = async (callback, errorToThrowIfTestSucceeds) => {
if (callback.length > 0) {
throw Error(
'Gated test helpers do not support the `done` callback. Return a ' +
'promise instead.'
);
}

// Install a global error event handler. We treat global error events as
// test failures, same as Jest's default behavior.
//
// Becaused we installed our own error event handler, Jest will not report a
// test failure. Conceptually it's as if we wrapped the entire test event in
// a try-catch.
let didError = false;
const errorEventHandler = () => {
didError = true;
};
// eslint-disable-next-line no-restricted-globals
if (typeof addEventListener === 'function') {
// eslint-disable-next-line no-restricted-globals
addEventListener('error', errorEventHandler);
}

try {
const maybePromise = callback();
if (
Expand All @@ -262,11 +279,20 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
// throws, we won't have captured it.
flushAllUnexpectedConsoleCalls();
} catch (testError) {
// Failed as expected
resetAllUnexpectedConsoleCalls();
return;
didError = true;
}
resetAllUnexpectedConsoleCalls();
// eslint-disable-next-line no-restricted-globals
if (typeof removeEventListener === 'function') {
// eslint-disable-next-line no-restricted-globals
removeEventListener('error', errorEventHandler);
}

if (!didError) {
// The test did not error like we expected it to. Report this to Jest as
// a failure.
throw errorToThrowIfTestSucceeds;
}
throw error;
};

const gatedErrorMessage = 'Gated test was expected to fail, but it passed.';
Expand Down

0 comments on commit 29a6ca3

Please sign in to comment.