diff --git a/README.md b/README.md index 95d9d18..3f2a135 100755 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ Try it out by cloning [cypress-terminal-report-demo](https://github.com/archfz/c - `console.log` usage was never meant to be used in the cypress test code. Using it will not log anything with this plugin. Using it also goes against the queue nature of cypress. Use `cy.log` instead. [See here for more details](https://github.com/archfz/cypress-terminal-report/issues/67). +- Using cypress-fail-fast and logging to files does not work out of the box. To enable support use +the [`logToFilesOnAfterRun`](#optionslogtofilesonafterrun) option. ## Install @@ -119,6 +121,12 @@ Callback to collect each test case's logs after its run. The first argument contains information about the test: the `spec` (test file), `test` (test title) and `state` (test state) fields. The second argument contains the test logs. 'type' is from the same list as for the `collectTypes` support install option (see below). Severity can be of ['', 'error', 'warning']. +#### `options.logToFilesOnAfterRun` +boolean; default: false; +When set to true it enables additional log write pass to files using the cypress [`after:run`](https://docs.cypress.io/api/plugins/after-run-api) plugin +hook. This option can only be used with cypress 6.2.0 onwards, and with the additional +`experimentalRunEvents` configuration on versions smaller than 6.7.0. +
### _Options for the support install_ @@ -284,6 +292,10 @@ directory. You should add `it.only` to the test case you are working on to speed ## Release Notes +#### 3.2.0 + +- Introduce support for [cypress-fail-fast](https://github.com/javierbrea/cypress-fail-fast) with the [logToFilesOnAfterRun](#optionslogtofilesonafterrun) option. [issue](https://github.com/archfz/cypress-terminal-report/issues/91) + #### 3.1.2 - Fix issue with duplicated log send on extended control when parent suite has after each but current suite doesn't. [issue](https://github.com/archfz/cypress-terminal-report/issues/98) diff --git a/src/installLogsPrinter.js b/src/installLogsPrinter.js index 3502a94..21a5fdd 100755 --- a/src/installLogsPrinter.js +++ b/src/installLogsPrinter.js @@ -37,7 +37,7 @@ const LOG_SYMBOLS = (() => { } })(); -let allMessages = {}; +let writeToFileMessages = {}; let outputProcessors = []; /** @@ -53,7 +53,7 @@ function installLogsPrinter(on, options = {}) { const result = tv4.validateMultiple(options, schema); if (!result.valid) { - throw new Error(`[cypress-terminal-report] Invalid plugin install options: ${tv4ErrorTransformer.toReadableString(result.errors)}`); + throw new CtrError(`Invalid plugin install options: ${tv4ErrorTransformer.toReadableString(result.errors)}`); } on('task', { @@ -73,8 +73,8 @@ function installLogsPrinter(on, options = {}) { options.printLogsToFile === "always" || isHookAndShouldLog ) { - allMessages[data.spec] = allMessages[data.spec] || {}; - allMessages[data.spec][data.test] = messages; + writeToFileMessages[data.spec] = writeToFileMessages[data.spec] || {}; + writeToFileMessages[data.spec][data.test] = messages; } } @@ -93,14 +93,7 @@ function installLogsPrinter(on, options = {}) { return null; }, [CONSTANTS.TASK_NAME_OUTPUT]: () => { - outputProcessors.forEach((processor) => { - if (Object.entries(allMessages).length !== 0){ - processor.write(allMessages); - if (options.outputVerbose !== false) - logOutputTarget(processor); - } - }); - allMessages = {}; + logToFiles(options); return null; } }); @@ -108,6 +101,27 @@ function installLogsPrinter(on, options = {}) { if (options.outputTarget) { installOutputProcessors(on, options); } + + if (options.logToFilesOnAfterRun) { + enableLogToFilesOnAfterRun(on, options); + } +} + +function enableLogToFilesOnAfterRun(on, options) { + on('after:run', () => { + logToFiles(options); + }); +} + +function logToFiles(options) { + outputProcessors.forEach((processor) => { + if (Object.entries(writeToFileMessages).length !== 0){ + processor.write(writeToFileMessages); + if (options.outputVerbose !== false) + logOutputTarget(processor); + } + }); + writeToFileMessages = {}; } diff --git a/src/installLogsPrinter.schema.json b/src/installLogsPrinter.schema.json index 2275bcd..d8b62d5 100644 --- a/src/installLogsPrinter.schema.json +++ b/src/installLogsPrinter.schema.json @@ -55,6 +55,9 @@ }, "collectTestLogs": { "type": "function" + }, + "logToFilesOnAfterRun": { + "type": "boolean" } }, "dependencies": { diff --git a/test/cypress/plugins/index.js b/test/cypress/plugins/index.js index 6c26044..acaddee 100755 --- a/test/cypress/plugins/index.js +++ b/test/cypress/plugins/index.js @@ -85,6 +85,9 @@ module.exports = (on, config) => { options.collectTestLogs = (context, logs) => console.log(`Collected ${logs.length} logs for test "${context.test}", last log: ${logs[logs.length - 1]}`); } + if (config.env.logToFilesOnAfterRun == '1') { + options.logToFilesOnAfterRun = true; + } if (config.env.enableCucumber) { on('file:preprocessor', cucumber()); @@ -92,6 +95,10 @@ module.exports = (on, config) => { config.testFiles = '**/*.{feature,features}'; } + if (config.env.failFast) { + require("cypress-fail-fast/plugin")(on, config); + } + require('../../../src/installLogsPrinter')(on, options); return config; diff --git a/test/cypress/support/index.js b/test/cypress/support/index.js index d6c3014..7bc7c55 100755 --- a/test/cypress/support/index.js +++ b/test/cypress/support/index.js @@ -3,6 +3,10 @@ import './commands'; const env = Cypress.env(); let config = {}; +if (env.failFast == '1') { + require("cypress-fail-fast"); +} + if (env.setLogTypes == '1') { config.collectTypes = ['cy:request', 'cy:log', 'cons:warn']; } diff --git a/test/output/out.spec.failFast.cst b/test/output/out.spec.failFast.cst new file mode 100644 index 0000000..5375385 --- /dev/null +++ b/test/output/out.spec.failFast.cst @@ -0,0 +1,2 @@ +Specs: +cypress/integration/requests.spec.js \ No newline at end of file diff --git a/test/output/out.spec.failFast.json b/test/output/out.spec.failFast.json new file mode 100644 index 0000000..b2bf9b9 --- /dev/null +++ b/test/output/out.spec.failFast.json @@ -0,0 +1,31 @@ +{ + "cypress/integration/requests.spec.js": { + "Requests. -> GET 200": [ + { + "type": "cy:request", + "severity": "success", + "message": "https://jsonplaceholder.cypress.io/todos/1\nStatus: 200\nResponse body: {\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"delectus aut autem\",\n \"completed\": false\n}" + }, + { + "type": "cy:request", + "severity": "success", + "message": "GET https://jsonplaceholder.cypress.io/todos/2\nStatus: 200\nResponse body: {\n \"userId\": 1,\n \"id\": 2,\n \"title\": \"quis ut nam facilis et officia qui\",\n \"completed\": false\n}" + }, + { + "type": "cy:request", + "severity": "success", + "message": "GET https://jsonplaceholder.cypress.io/todos/3\nStatus: 200\nResponse body: {\n \"userId\": 1,\n \"id\": 3,\n \"title\": \"fugiat veniam minus\",\n \"completed\": false\n}" + }, + { + "type": "cy:command", + "severity": "error", + "message": "get\t.breaking-get" + }, + { + "type": "cy:command", + "severity": "success", + "message": "task\tfailFastShouldSkip, true" + } + ] + } +} \ No newline at end of file diff --git a/test/output/out.spec.failFast.txt b/test/output/out.spec.failFast.txt new file mode 100644 index 0000000..48dd8bc --- /dev/null +++ b/test/output/out.spec.failFast.txt @@ -0,0 +1,29 @@ +cypress/integration/requests.spec.js: + Requests. -> GET 200 + cy:request (K): https://jsonplaceholder.cypress.io/todos/1 + Status: 200 + Response body: { + "userId": 1, + "id": 1, + "title": "delectus aut autem", + "completed": false + } + cy:request (K): GET https://jsonplaceholder.cypress.io/todos/2 + Status: 200 + Response body: { + "userId": 1, + "id": 2, + "title": "quis ut nam facilis et officia qui", + "completed": false + } + cy:request (K): GET https://jsonplaceholder.cypress.io/todos/3 + Status: 200 + Response body: { + "userId": 1, + "id": 3, + "title": "fugiat veniam minus", + "completed": false + } + cy:command (X): get .breaking-get + cy:command (K): task failFastShouldSkip, true + diff --git a/test/package-lock.json b/test/package-lock.json index 902a7f2..811fe0e 100644 --- a/test/package-lock.json +++ b/test/package-lock.json @@ -2662,6 +2662,12 @@ } } }, + "cypress-fail-fast": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/cypress-fail-fast/-/cypress-fail-fast-2.3.2.tgz", + "integrity": "sha512-FTP9gpUF3w5s7oWDwli0pEnijy6H/ek/nrSIL1ZOb2GRWR9WIhc42eDN5cUnPu5gIpzwHvkxpKmBGd6Z5U+6Wg==", + "dev": true + }, "d": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/d/-/d-1.0.1.tgz", diff --git a/test/package.json b/test/package.json index 0c9ab91..11fdfc2 100755 --- a/test/package.json +++ b/test/package.json @@ -29,5 +29,8 @@ "fs-extra": "^9.0.1", "glob": "^7.1.6", "mocha": "^8.3.2" + }, + "devDependencies": { + "cypress-fail-fast": "^2.3.2" } } diff --git a/test/specs/outputToFiles.spec.js b/test/specs/outputToFiles.spec.js index ea2c172..94126e6 100755 --- a/test/specs/outputToFiles.spec.js +++ b/test/specs/outputToFiles.spec.js @@ -99,4 +99,15 @@ describe('Output to files.', () => { }); }).timeout(90000); + it('Should still output to files when fail fast is installed and log to files on after run enabled.', async () => { + const outRoot = {}; + const testOutputs = {}; + outputCleanUpAndInitialization(testOutputs, outRoot); + + const specFiles = ['requests.spec.js']; + await runTest(commandBase(['failFast=1', 'generateOutput=1', 'logToFilesOnAfterRun=1'], specFiles), (error, stdout, stderr) => { + expectOutputFilesToBeCorrect(testOutputs, outRoot, specFiles, 'failFast'); + }); + }).timeout(90000); + });