JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 45029
  • Score
    100M100P100Q162191F
  • License MIT

fail cypress test on console error

Package Exports

  • cypress-fail-on-console-error
  • cypress-fail-on-console-error/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cypress-fail-on-console-error) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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 [] 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, Config } from 'cypress-fail-on-console-error';

const config: 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
// cypressLog 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

Set config from cypress test

Use failOnConsoleError functions getConfig() and setConfig() with your own requirements. Detailed example implementation cypress comands & cypress test. Note that the config will be resetted to initial config between tests.

const { getConfig, setConfig } = failOnConsoleError(config);

Cypress.Commands.addAll({
    getExcludeMessages: () => cy.wrap(getConfig()),
    setExcludeMessages: (excludeMessages: (string | RegExp)[]) => 
        setConfig({ ...getConfig(), excludeMessages );
describe('example test', () => {
    it('should set excluded messages', () => {
        cy.setExcludeMessages(['foo', 'bar']).then(() => {
            cy.visit('./cypress/fixtures/consoleError.html');
        });
    });
});

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