What is cypress-log-to-output?
The cypress-log-to-output npm package allows you to capture and output Cypress logs to the terminal or other output streams. This is particularly useful for debugging and monitoring tests in CI environments where you may not have access to the browser's console.
What are cypress-log-to-output's main functionalities?
Log to Terminal
This feature allows you to capture Cypress logs and print them directly to the terminal. This is useful for debugging tests in environments where you don't have access to the browser's console.
const installLogsPrinter = require('cypress-log-to-output').install;
installLogsPrinter();
Custom Log Filtering
This feature allows you to filter logs based on custom criteria. In this example, only logs that contain the word 'error' will be printed to the terminal.
const installLogsPrinter = require('cypress-log-to-output').install;
installLogsPrinter({
filterLog: (log) => log.message.includes('error')
});
Log to File
This feature allows you to capture Cypress logs and write them to a file. This is useful for keeping a persistent record of logs for later analysis.
const fs = require('fs');
const installLogsPrinter = require('cypress-log-to-output').install;
installLogsPrinter({
printLogsToFile: 'cypress-logs.txt'
});
Other packages similar to cypress-log-to-output
cypress-failed-log
The cypress-failed-log package captures logs for failed Cypress tests and saves them to a file. Unlike cypress-log-to-output, it focuses specifically on failed tests and provides detailed logs for debugging.
cypress-terminal-report
The cypress-terminal-report package captures Cypress logs and outputs them to the terminal, similar to cypress-log-to-output. However, it also provides additional features like grouping logs by test and adding custom log messages.
cypress-log-to-output
This is a Cypress plugin that sends all console logs that occur in the browser to stdout in the terminal. This means that you can see any kind of console.log
, console.info
or console.error
that occurs in the browser, even if your tests are running in the terminal.
Installation
npm install --save-dev cypress-log-to-output
Usage
In your cypress/plugins/index.js
, add this to your module.exports
:
module.exports = (on, config) => {
require('cypress-log-to-output').install(on)
}
You'll now see all browser console logs in your terminal output.
cypress run --browser=chrome
Works in Chrome, Chromium, or Canary browsers during cypress run
and cypress open
.
Electron is not currently supported. I can't find a way to attach the Chrome Debugging Protocol to the Electron browser spawned by Cypress.
Filtering Events
If you want to filter events, you can use a custom filtering callback:
module.exports = (on, config) => {
require('cypress-log-to-output').install(on, (type, event) => {
if (event.level === 'error' || event.type === 'error') {
return true
}
return false
})
}
Recording Logs
If you want to record the logs internally, you can use the recordLogs
option:
module.exports = (on, config) => {
const options = { recordLogs: true };
require('cypress-log-to-output').install(on, filterCallback, options)
}
The logs will be stored in an internal buffer. They can be accessed using the getLogs
exported function.
The buffer can be cleared using the clearLogs
exported function.
Disabling debug info
You can remove the lines beginning with [cypress-log-to-output]
by passing -cypress-log-to-output
in the DEBUG
environment variable.