Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gate pragma to detect global error events #28591

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

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