@mknrt/cypress-console-spy
v1.2.6
Published
Cypress plugin to monitor and handle console errors and warnings
Maintainers
Readme
@mknrt/cypress-console-spy
Cypress plugin for tracking browser console output, uncaught browser errors, file logging, and aggregated run statistics.
Installation
npm install @mknrt/cypress-console-spySetup
Register the server side in cypress.config.js:
const { defineConfig } = require('cypress');
const { server } = require('@mknrt/cypress-console-spy');
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
server(on, config);
return config;
},
expose: {
consoleDaemon: {
failOnSpy: true,
logToFile: true,
logDir: 'cypress/logs',
methodsToTrack: ['error', 'warn'],
throwOnWarning: false,
whitelist: ['socket.io', /ResizeObserver/],
debug: false,
},
},
},
});Register the client side in cypress/support/e2e.js:
const { client } = require('@mknrt/cypress-console-spy');
client(Cypress, Cypress.expose('consoleDaemon'));If your project still stores plugin config in env.consoleDaemon, that object can still be passed to client(). The plugin itself only consumes the config object you provide.
Configuration
consoleDaemon supports:
failOnSpy: fail the test when non-whitelisted console issues are foundlogToFile: write filtered issues to a spec-specific log filelogDir: custom server-side log directorymethodsToTrack: any subset of['error', 'warn', 'log', 'info', 'debug']throwOnWarning: makeconsole.warnfail the test toowhitelist: strings or regular expressions ignored during filteringdebug: print plugin internals to the browser console
Suite And Test Overrides
Overrides must be nested under consoleDaemon.
describe(
'suite override',
{
viewportWidth: 900,
consoleDaemon: {
failOnSpy: false,
whitelist: ['known noisy error'],
},
},
() => {
it(
'test override',
{
consoleDaemon: {
methodsToTrack: ['warn', 'info'],
logToFile: false,
},
},
() => {
cy.window().then((win) => {
win.console.warn('tracked warning');
win.console.info('tracked info');
});
},
);
},
);Nested describe, describe.only, and it.only keep the same consoleDaemon semantics as plain describe / it.
Behavior
error,warn,log,info, anddebugcan all be tracked.- After whitelist filtering, all tracked issues are sent to server-side statistics and optional file logs.
- Test failure is controlled separately:
console.errorfails whenfailOnSpyistrueconsole.warnfails only when bothfailOnSpyandthrowOnWarningaretruelog,info, anddebugnever fail the test by themselves
- Uncaught browser
errorevents are captured aserrorissues.
Statistics
cy.task('getErrorStats') returns:
{
errors: 0,
warnings: 0,
info: 0,
details: [
{ type: 'error', message: '...' },
{ type: 'warn', message: '...' },
{ type: 'info', message: '...' },
],
}Use cy.task('resetErrorStats') to reset counters between scenarios.
Logging
- Log directories are created automatically.
- Log file names are derived from the full relative spec path, not only the basename.
logToFile: falsecan be overridden at suite or test level.
Legacy Tasks
The plugin still exposes these legacy tasks for backward compatibility:
logConsoleErrorsaveConsoleErrorToFilenotifyCriticalErrorgetErrorStatsresetErrorStatssetDebugMode
License
MIT
