cypress-fail-on-network-request

fail cypress test on network request


Keywords
cypress, testing, network-request
License
MIT
Install
npm install cypress-fail-on-network-request@1.0.1

Documentation

cypress-fail-on-network-error

🔥 Don´t be shy and give it a try. This plugin is brand new! 🔥

The plugin observes all network requests from your configuration. Cypress test will fail when the error conditions are met. For observing console.error() please check out cypress-fail-on-console-error.

Installation

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

Usage

cypress/support/e2e.js

import failOnNetworkError, { Config, Request } from 'cypress-fail-on-network-error';

const config: Config = {
    requests: [
        'simpleUrlToExclude',
        { url: 'simpleUrlToExclude', method: 'GET', status: 400 },
        { url: /urlToExclude/, method: 'POST', status: 428 },
        { status: 430 },
        { status: { from: 200, to: 399 } },
    ],
};

failOnNetworkError(config)

Config

Parameter Default
Description
requests [] Exclude requests from throwing AssertionError. Types string, RegExp, Request are accepted. string and request.url will be converted to type RegExp. String.match() will be used for matching.

Set config from cypress test

Use failOnNetworkError 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 } = failOnNetworkError(config);

Cypress.Commands.addAll({
    getConfigRequests: () => {
        return cy.wrap(getConfig().requests);
    },
    setConfigRequests: (requests: (string | Request)[]) => {
        setConfig({ ...getConfig(), requests });
    },
});
describe('example test', () => {
    it('should set exclude requests', () => {
        cy.setConfigRequests(['urlToExclude']);
        cy.visit('url');
    });
});

Wait for all pending requests to be resolved

Use failOnNetworkError function waitForRequests() to wait until all pending requests are resolved. The default timeout is 10000 ms which can be changed by overriding the default value waitForRequests(5000). When reaching the timeout, Cypress test execution will continue without throwing an timeout exception. Detailed documenation for cypress comands & cypress test.

const { waitForRequests } = failOnNetworkError(config);

Cypress.Commands.addAll({
    waitForRequests: () => waitForRequests(),
});
describe('example test', () => {
    it('should wait for requests to be solved', () => {
        cy.visit('url');
        cy.waitForRequests();
    });
});

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