1.0.2 • Published 1 year ago

cypress-fail-on-network-request v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

cypress-fail-on-network-request

This Plugin observes network requests through cypress network events. Cypress test fails when a response or request error is received. For observing console.error() please check out cypress-fail-on-console-error.

Installation

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

Usage

cypress/support/e2e.js

import failOnNetworkRequest, { Config, Request } from 'cypress-fail-on-network-request';

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

failOnNetworkRequest(config)

Config

ParameterDefaultDescription
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 failOnNetworkRequest 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 } = failOnNetworkRequest(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 failOnNetworkRequest 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 } = failOnNetworkRequest(config);

Cypress.Commands.addAll({
    waitForRequests: () => waitForRequests(),
});
describe('example test', () => {
    it('should wait for requests to be solved', () => {
        cy.visit('url');
        cy.wait(0).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
1.0.2

1 year ago

1.0.1

2 years ago

1.0.0

2 years ago