/cypress-fail-on-console-error

tail cypress test on console error

Primary LanguageTypeScriptMIT LicenseMIT

cypress-fail-on-console-error

This Plugin observes console.error() function from window object. Cypress test will fail when the error function gets executed.

Installation

npm install cypress-fail-on-console-error --save-dev

Usage

cypress/support/e2e.js

import failOnConsoleError from 'cypress-fail-on-console-error';

failOnConsoleError();

Config (optional)

Parameter Default
Description
excludeMessages undefined Exclude console messages from throwing AssertionError. Regular expression parameters are acceptable. String parameters will be interpreted as regular expression. String.match() will be used for evaluation. Be sure to escape the string regular expression for special characters. When console message property stacktrace exists, then the whole stacktrace can be matched.
includeConsoleTypes [consoleType.ERROR] Define console types for observation
cypressLog false Enable debug logs for errorMessage_excludeMessage_match and errorMessage_excluded to cypress runner

import failOnConsoleError, { consoleType } from 'cypress-fail-on-console-error';

const config = {
    excludeMessages: ['foo', /^some bar-regex.*/],
    includeConsoleTypes: [
        consoleType.ERROR,
        consoleType.WARN,
        consoleType.INFO,
    ],
    cypressLog: true,
};

failOnConsoleError(config);

// excludeMessages[0] matches example console message 'this is a foo message'
// excludeMessages[1] matches example console message 'some bar-regex message'
// includeConsoleTypes observe console types ERROR, WARN and INFO
//debug information will be printed to the cypress runner

Using Javascript, consoleType Enum can be parsed as number values

failOnConsoleError({
    includeConsoleTypes: [0, 1, 2],
});

// 0 = INFO
// 1 = WARN
// 2 = ERROR

Debugging

When Cypress log is activated, debug information about the matching and exclude process are available from the cypress runner. As a plus, the generated error message string can be verified. image info

Contributing

  1. Create an project issue with proper description and expected behaviour
  2. Provide a PR with implementation and tests. Command npm run verify have to pass locally