Support for custom log collector function
peruukki opened this issue ยท 10 comments
We are using mochawesome to generate Cypress test run reports, and I was able to include the logs from cypress-terminal-report
in the report by adding support for a custom collectTestLogs
function that receives each test case's logs, with this patch:
diff --git a/node_modules/cypress-terminal-report/src/installLogsCollector.js b/node_modules/cypress-terminal-report/src/installLogsCollector.js
index 5dba191..1efbe27 100755
--- a/node_modules/cypress-terminal-report/src/installLogsCollector.js
+++ b/node_modules/cypress-terminal-report/src/installLogsCollector.js
@@ -97,6 +97,9 @@ function installLogsCollector(config = {}) {
messages: logs,
state: this.currentTest.state
}, {log: false});
+ if (config.collectTestLogs) {
+ config.collectTestLogs(this, logs);
+ }
});
after(function () {
@@ -119,6 +122,9 @@ function validateConfig(config) {
throw new Error(`[cypress-terminal-report] Invalid plugin install options: ${tv4ErrorTransformer.toReadableString(result.errors)}`);
}
+ if (config.collectTestLogs && typeof config.collectTestLogs !== 'function') {
+ throw new CtrError(`[cypress-terminal-report] Collect test logs option expected to be a function.`);
+ }
if (config.filterLog && typeof config.filterLog !== 'function') {
throw new CtrError(`[cypress-terminal-report] Filter log option expected to be a function.`);
}
diff --git a/node_modules/cypress-terminal-report/src/installLogsCollector.schema.json b/node_modules/cypress-terminal-report/src/installLogsCollector.schema.json
index 2525c67..38aaf37 100644
--- a/node_modules/cypress-terminal-report/src/installLogsCollector.schema.json
+++ b/node_modules/cypress-terminal-report/src/installLogsCollector.schema.json
@@ -25,6 +25,9 @@
]
}
},
+ "collectTestLogs": {
+ "type": "function"
+ },
"filterLog": {
"type": "function"
},
I use it like this in my Cypress support file:
const installLogsCollector = require('cypress-terminal-report/src/installLogsCollector');
const addContext = require('mochawesome/addContext');
installLogsCollector({
collectTestLogs: (test, logs) => addContext(test, { title: 'Cypress logs', value: renderCypressLogs(logs) }),
});
Cypress.on('test:after:run', (test, runnable) => {
if (test.state === 'failed' && runnable.context) {
addContext({ test }, runnable.context);
}
});
Would you be interested in supporting this kind of an option? This is neither terminal nor file output, but your library gives us the information we need (command log and browser logs), and it formats the information beautifully. ๐
Related to this, my renderCypressLogs
function above is just a modified version of your logToTerminal
function that returns a string instead of writing to the console (and uses less padding). In our use case it would be useful if the log message formatting function that you use was exposed so we could use it directly, but I don't know how useful that would be for others. ๐
Idea sounds good. I guess it could be like an api for different log targets. I think it would be best though if this was exposed on node level so in the installLogsPrinter. If you already saw the source code you could easily add this feature, besides the same changes you've shown you'd only need to add the option in the docbloc and in the validation schema and add test for it.
Thanks for the quick feedback! I agree that this would be more fitting in installLogsPrinter
but as far as I know, we can't do this on the Node side because we don't have the same Mocha test instance available there that is eventually used by Mochawesome. Maybe it would be useful in both contexts, browser and Node?
Yeah we can add it on both and let the client code decide which to use. One should choose node side for performance and access to filesystem etc etc.
I started doing this but I don't know how I can test the browser-side callback function because it is run in the afterEach
hook and doesn't have any effect on the Cypress output (I event tried cy.log()
in the callback but it doesn't appear in the output). I also can't spy on it because the tests run Cypress as an external process. Do you have any suggestions how to test it?
Maybe you can try console.log that data, that might appear. Otherwise I would suggest testing only the node js side exposed config and leave a todo for a test on browser side since we also have this issue #55
One other reason that might cause that these logs do not appear is that atleast in the diff you have provided you are calling the callback after the messages get sent to node side and log, thus these new ones get added to late.
One other reason that might cause that these logs do not appear is that atleast in the diff you have provided you are calling the callback after the messages get sent to node side and log, thus these new ones get added to late.
Ah yes, moving the callback call before cy.task
worked, now cy.log
s in the callback are shown in the output, thanks!
Sorry, I was able to do the browser side callback but struggled to find a place for this in the Node-side API. Now I had another look, and it seems this can already be achieved Node-side like this:
on('task', {
ctrLogMessages: (data) => { /* ... */ }
});
Should this possibility just be documented, or can we provide some more convenient way to do the same? I guess I can add a similar collectTestLogs
to the plugin options and get your feedback on it.
Released in 2.3.0