Restrict Cypress uncaught exception suppression to known benign errors
Co-Authored-By: Codex <codex@openai.com>
This commit is contained in:
@@ -20,9 +20,11 @@
|
|||||||
|
|
||||||
// Import commands.js using ES2015 syntax:
|
// Import commands.js using ES2015 syntax:
|
||||||
import "./commands";
|
import "./commands";
|
||||||
Cypress.on("uncaught:exception", (err, runnable) => {
|
import { getCypressUncaughtExceptionDecision } from "./uncaughtExceptionPolicy";
|
||||||
// returning false here prevents Cypress from failing the test
|
|
||||||
return false;
|
Cypress.on("uncaught:exception", (error) => {
|
||||||
|
// Ignore only known benign browser/runtime noise; fail all other app errors.
|
||||||
|
return getCypressUncaughtExceptionDecision(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Alternatively you can use CommonJS syntax:
|
// Alternatively you can use CommonJS syntax:
|
||||||
|
|||||||
26
cypress/support/uncaughtExceptionPolicy.js
Normal file
26
cypress/support/uncaughtExceptionPolicy.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
// The Lucia project.
|
||||||
|
// Copyright 2026-2026 DSP, inc. All rights reserved.
|
||||||
|
// Authors:
|
||||||
|
// imacat.yang@dsp.im (imacat), 2026/03/08
|
||||||
|
/**
|
||||||
|
* Returns whether an uncaught exception should be ignored in Cypress.
|
||||||
|
* @param {unknown} error - The thrown uncaught exception.
|
||||||
|
* @returns {boolean} True when the exception is known-benign and safe to ignore.
|
||||||
|
*/
|
||||||
|
export function shouldIgnoreUncaughtException(error) {
|
||||||
|
const message = error instanceof Error ? error.message : String(error);
|
||||||
|
const ignorePatterns = [
|
||||||
|
/ResizeObserver loop limit exceeded/i,
|
||||||
|
/ResizeObserver loop completed with undelivered notifications/i,
|
||||||
|
];
|
||||||
|
return ignorePatterns.some((pattern) => pattern.test(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts ignore policy into Cypress uncaught-exception callback return value.
|
||||||
|
* @param {unknown} error - The thrown uncaught exception.
|
||||||
|
* @returns {false|undefined} False to ignore, undefined to let Cypress fail the test.
|
||||||
|
*/
|
||||||
|
export function getCypressUncaughtExceptionDecision(error) {
|
||||||
|
return shouldIgnoreUncaughtException(error) ? false : undefined;
|
||||||
|
}
|
||||||
31
tests/unit/cypress/uncaughtExceptionPolicy.test.js
Normal file
31
tests/unit/cypress/uncaughtExceptionPolicy.test.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
getCypressUncaughtExceptionDecision,
|
||||||
|
shouldIgnoreUncaughtException,
|
||||||
|
} from "../../../cypress/support/uncaughtExceptionPolicy";
|
||||||
|
|
||||||
|
describe("shouldIgnoreUncaughtException", () => {
|
||||||
|
it("returns true for known benign ResizeObserver errors", () => {
|
||||||
|
expect(
|
||||||
|
shouldIgnoreUncaughtException(new Error("ResizeObserver loop limit exceeded")),
|
||||||
|
).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns false for unknown runtime errors", () => {
|
||||||
|
expect(shouldIgnoreUncaughtException(new Error("TypeError: x is undefined"))).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns false for Cypress handler when the error should be ignored", () => {
|
||||||
|
expect(
|
||||||
|
getCypressUncaughtExceptionDecision(
|
||||||
|
new Error("ResizeObserver loop completed with undelivered notifications."),
|
||||||
|
),
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns undefined for Cypress handler when the error should fail the test", () => {
|
||||||
|
expect(
|
||||||
|
getCypressUncaughtExceptionDecision(new Error("TypeError: x is undefined")),
|
||||||
|
).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user