Skip to content

Commit

Permalink
#91: Introduce support of logging to files for cypress fail fast.
Browse files Browse the repository at this point in the history
  • Loading branch information
archfz committed May 17, 2021
1 parent ef88ebe commit 33497c6
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 12 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
<br/>
### _Options for the support install_
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 26 additions & 12 deletions src/installLogsPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const LOG_SYMBOLS = (() => {
}
})();

let allMessages = {};
let writeToFileMessages = {};
let outputProcessors = [];

/**
Expand All @@ -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', {
Expand All @@ -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;
}
}

Expand All @@ -93,21 +93,35 @@ 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;
}
});

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 = {};
}


Expand Down
3 changes: 3 additions & 0 deletions src/installLogsPrinter.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
},
"collectTestLogs": {
"type": "function"
},
"logToFilesOnAfterRun": {
"type": "boolean"
}
},
"dependencies": {
Expand Down
7 changes: 7 additions & 0 deletions test/cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,20 @@ 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());
config.ignoreTestFiles = '*.js';
config.testFiles = '**/*.{feature,features}';
}

if (config.env.failFast) {
require("cypress-fail-fast/plugin")(on, config);
}

require('../../../src/installLogsPrinter')(on, options);

return config;
Expand Down
4 changes: 4 additions & 0 deletions test/cypress/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand Down
2 changes: 2 additions & 0 deletions test/output/out.spec.failFast.cst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Specs:
cypress/integration/requests.spec.js
31 changes: 31 additions & 0 deletions test/output/out.spec.failFast.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
29 changes: 29 additions & 0 deletions test/output/out.spec.failFast.txt
Original file line number Diff line number Diff line change
@@ -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

6 changes: 6 additions & 0 deletions test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@
"fs-extra": "^9.0.1",
"glob": "^7.1.6",
"mocha": "^8.3.2"
},
"devDependencies": {
"cypress-fail-fast": "^2.3.2"
}
}
11 changes: 11 additions & 0 deletions test/specs/outputToFiles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

});

0 comments on commit 33497c6

Please sign in to comment.