From d4bb81303307f02f7457d1cdc65aa11de9aa30e0 Mon Sep 17 00:00:00 2001 From: Mike Lewis Date: Tue, 25 Jul 2023 16:46:48 +0000 Subject: [PATCH] Make module_test runner & example handle failures correctly The existing runner.js wouldn't exit w/ a non-zero code if either a test fails, or cypress.run threw an exception. This will handle both cases Fixes #44 --- cypress/tests/module_test/runner.js | 31 ++++++++++++++++++++++------- docs/rules.md | 29 +++++++++++++++++++++------ 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/cypress/tests/module_test/runner.js b/cypress/tests/module_test/runner.js index 8f5c773..d0eefc6 100644 --- a/cypress/tests/module_test/runner.js +++ b/cypress/tests/module_test/runner.js @@ -1,9 +1,26 @@ const cypress = require('cypress') -cypress.run({ - headless: true, -}).then(result => { - if (result.status === 'failed') { - process.exit(1); - } -}) \ No newline at end of file +async function main() { + try { + const result = await cypress.run({ + headless: true, + }) + + // If any tests have failed, results.failures is non-zero, some tests have failed + if (result.failures) { + console.error('One or more cypress tests have failed') + console.error(result.message) + process.exit(1) + } + + if (result.status !== 'finished') { + console.error('Cypress tests failed with status', result.status) + process.exit(2) + } + } catch (e) { + console.error("Cypress encountered unexpected exception. Exiting.", e) + process.exit(3) + } +} + +main(); diff --git a/docs/rules.md b/docs/rules.md index 9a146cc..c84e9f7 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -20,13 +20,30 @@ Example `runner.js`: ``` const cypress = require('cypress') -cypress.run({ -headless: true, -}).then(result => { -if (result.status === 'failed') { - process.exit(1); +async function main() { + try { + const result = await cypress.run({ + headless: true, + }) + + // If any tests have failed, results.failures is non-zero, some tests have failed + if (result.failures) { + console.error('One or more cypress tests have failed') + console.error(result.message) + process.exit(1) + } + + if (result.status !== 'finished') { + console.error('Cypress tests failed with status', result.status) + process.exit(2) + } + } catch (e) { + console.error("Cypress encountered unexpected exception. Exiting.", e) + process.exit(3) + } } -}) + +main(); ```