This Plugin observes console.error()
function from window object. Cypress test will fail when the error function gets executed. For observing network errors please check out cypress-fail-on-network-requests.
npm install cypress-fail-on-console-error --save-dev
cypress/support/e2e.js
import failOnConsoleError from 'cypress-fail-on-console-error';
failOnConsoleError();
Parameter | Default | Description |
---|---|---|
consoleMessages |
[] |
Exclude console messages from throwing AssertionError . Types RegExp and string are accepted. Strings will be converted to regular expression. RegExp.test() will be used for console message matching. Make sure to escape special characters. When console message property stacktrace exists, then the whole stacktrace can be matched. |
consoleTypes |
['error'] |
Define console types for observation. error , warn and info are accepted values. |
debug |
false |
Enable debug logs for consoleMessage_configConsoleMessage_match and consoleMessage_excluded to cypress runner |
import failOnConsoleError, { Config } from 'cypress-fail-on-console-error';
const config: Config = {
consoleMessages: ['foo', /^some bar-regex.*/],
consoleTypes: ['error', 'warn', 'info'],
debug: false,
};
failOnConsoleError(config);
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.
// Simple example implementation
const { getConfig, setConfig } = failOnConsoleError(config);
Cypress.Commands.addAll({
getConsoleMessages: () => cy.wrap(getConfig().consoleMessages),
setConsoleMessages: (consoleMessages: (string | RegExp)[]) =>
setConfig({ ...getConfig(), consoleMessages });
describe('example test', () => {
it('should set console messages', () => {
cy.setConsoleMessages(['foo', 'bar']);
cy.visit('...');
});
});
When Cypress log is activated, debug information about the console messages / config console messages matching and excluding process are available from the cypress runner. As a plus, the generated error message string can be verified.
- Create an project issue with proper description and expected behaviour
- NPM command
npm run verify
have to pass locally - Provide a PR with implementation and tests