Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Decouple the cleanup logic from running Cypress (#66236)

Nemanja Glumacdc89b2bbeb63
diff --git a/e2e/runner/cypress-node-js-runner.js b/e2e/runner/cypress-node-js-runner.js
index eb87b2f2181..5f107195f4b 100644
--- a/e2e/runner/cypress-node-js-runner.js
+++ b/e2e/runner/cypress-node-js-runner.js
@@ -1,40 +1,42 @@
const cypress = require("cypress");
-const { FAILURE_EXIT_CODE } = require("./constants/exit-code");
-
/**
- * This simply runs Cypress through the Cypress Module API rather than the CLI.
+ * Runs Cypress through the Module API and throws on any failure.
* See: https://docs.cypress.io/app/references/module-api
*
- * @param {Partial<CypressCommandLine.CypressRunOptions> | Partial<CypressCommandLine.CypressOpenOptions>} options - Cypress configuration options for running or opening tests
- * @param {function(number): Promise<void>} exitFunction - Async function to call on exit with the exit code
- * @returns {Promise<CypressCommandLine.CypressRunResult | CypressCommandLine.CypressFailedRunResult | void>}
- * @throws {Error} When Cypress fails to run tests
+ * Errors are propagated to the parent process rather than handled here.
+ * The caller is responsible for catching errors and performing cleanup.
+ *
+ * This wrapper exists because cypress.run() resolves successfully even when
+ * tests fail - it only throws when Cypress itself fails to start (e.g., invalid
+ * config). We convert test failures into thrown errors for consistent handling.
+ *
+ * @param {Partial<CypressCommandLine.CypressRunOptions> | Partial<CypressCommandLine.CypressOpenOptions>} options - Cypress configuration options
+ * @returns {Promise<CypressCommandLine.CypressRunResult>} - Resolves with results if all tests pass
+ * @throws {Error} When Cypress fails to run (e.g., config error) or any tests fail
*/
-const runCypress = async (options, exitFunction) => {
+const runCypress = async (options) => {
const openMode = process.env.OPEN_UI === "true";
- try {
- const { status, message, totalFailed, failures } = openMode
- ? await cypress.open(options)
- : await cypress.run(options);
-
- // At least one test failed
- if (totalFailed > 0) {
- await exitFunction(FAILURE_EXIT_CODE);
- }
+ // Open mode is interactive - no results to validate
+ if (openMode) {
+ await cypress.open(options);
+ return;
+ }
- // Something went wrong and Cypress failed to even run tests
- if (status === "failed" && failures) {
- console.error(message);
+ const result = await cypress.run(options);
- await exitFunction(failures);
- }
- } catch (e) {
- console.error("Failed to run Cypress!\n", e);
+ // Cypress failed to even run tests (e.g., config error, browser crash)
+ if (result.status === "failed") {
+ throw new Error(`Cypress failed to run: ${result.message}`);
+ }
- await exitFunction(FAILURE_EXIT_CODE);
+ // Tests ran but at least one failed
+ if (result.totalFailed > 0) {
+ throw new Error(`${result.totalFailed} test(s) failed`);
}
+
+ return result;
};
module.exports = runCypress;
diff --git a/e2e/runner/run_cypress_ci.js b/e2e/runner/run_cypress_ci.js
index 9ea1b277f8f..dcafaac3a35 100755
--- a/e2e/runner/run_cypress_ci.js
+++ b/e2e/runner/run_cypress_ci.js
@@ -37,7 +37,7 @@ const startServer = async () => {
const runTests = async (config, cliArguments = []) => {
const userOverrides = await parseArguments(cliArguments);
- await runCypress({ ...config, ...userOverrides }, process.exit);
+ await runCypress({ ...config, ...userOverrides });
};
// Custom "modes"
diff --git a/e2e/runner/run_cypress_local.ts b/e2e/runner/run_cypress_local.ts
index 7be7f46ae8f..7d404da3fd5 100644
--- a/e2e/runner/run_cypress_local.ts
+++ b/e2e/runner/run_cypress_local.ts
@@ -131,10 +131,7 @@ const init = async () => {
printBold("⏳ Generating app db snapshots");
process.env.OPEN_UI = "false";
- await runCypress(
- { configFile: "e2e/support/cypress-snapshots.config.js" },
- cleanup,
- );
+ await runCypress({ configFile: "e2e/support/cypress-snapshots.config.js" });
process.env.OPEN_UI = `${options.OPEN_UI}`;
} else {
printBold("Skipping snapshot generation, beware of stale snapshot caches");
@@ -166,26 +163,22 @@ const init = async () => {
printBold("⏳ Starting Sample/Host App Cypress Tests");
const config = resolveSdkE2EConfig(options.SDK_TEST_SUITE);
- await runCypress(config, cleanup);
+ await runCypress(config);
}
if (options.CYPRESS_TESTING_TYPE === "component") {
printBold("⏳ Starting Cypress SDK component tests");
- await runCypress(
- {
- configFile:
- "e2e/support/cypress-embedding-sdk-component-test.config.js",
- testingType: "component",
- },
- cleanup,
- );
+ await runCypress({
+ configFile: "e2e/support/cypress-embedding-sdk-component-test.config.js",
+ testingType: "component",
+ });
}
if (options.CYPRESS_TESTING_TYPE === "e2e") {
const config = { configFile: "e2e/support/cypress.config.js" };
printBold("⏳ Starting Cypress");
- await runCypress({ ...config, ...userOverrides }, cleanup);
+ await runCypress({ ...config, ...userOverrides });
}
};