Registry / testing / cypress-fail-on-network-error

cypress-fail-on-network-error

JSON →
library1.0.5jsnpmunverified

cypress-fail-on-network-error is a Cypress plugin designed to automatically fail tests when network requests made during their execution encounter specified error conditions. This allows for proactive identification of backend issues, broken API endpoints, or unexpected HTTP status codes directly within the Cypress test suite. The current stable version is 1.0.5, with releases primarily focused on dependency updates (especially Cypress itself) and minor enhancements. It differentiates itself by offering fine-grained configuration to define which network errors (based on URL patterns, HTTP methods, and status codes or ranges) should trigger a test failure, and provides utilities to dynamically adjust this configuration during tests and wait for pending requests to resolve. This offers more control than relying solely on server-side logging or general test timeouts, providing immediate feedback on client-server interaction health.

npm install cypress-fail-on-network-error
INSTALL
IMPORT
SIG · CYPRESS-FAIL-ON-NE
C
cypress-fail-on-network-error
testingjavascriptv1.0.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

failOnNetworkError
import failOnNetworkError from 'cypress-fail-on-network-error';
const failOnNetworkError = require('cypress-fail-on-network-error');
The primary default export is a function that initializes the plugin with a configuration and returns an object containing utility methods like `getConfig`, `setConfig`, and `waitForRequests`. This package is primarily ESM.
Config, Request
import { Config, Request } from 'cypress-fail-on-network-error';
import Config from 'cypress-fail-on-network-error';
These are named type exports used for TypeScript type annotations when defining the plugin's configuration.
getConfig, setConfig, waitForRequests
const { getConfig, setConfig, waitForRequests } = failOnNetworkError(initialConfig);
import { getConfig, setConfig } from 'cypress-fail-on-network-error';
These utility functions are methods of the object returned when the default `failOnNetworkError` function is called with a configuration, not direct named exports from the module root. They allow for dynamic interaction with the plugin's state during a test run.

This quickstart demonstrates how to install `cypress-fail-on-network-error`, configure it in Cypress's e2e support file with specific error conditions and exclusions, and how to use its methods (exposed as Cypress commands) to control network error detection within your tests, including dynamically updating the configuration and waiting for requests.

npm install cypress-fail-on-network-error --save-dev // cypress/support/e2e.ts or e2e.js import failOnNetworkError, { Config, Request } from 'cypress-fail-on-network-error'; const initialConfig: Config = { requests: [ // Exclude specific URLs from causing failures 'http://example.com/api/healthcheck', { url: 'http://example.com/api/ignored-error', method: 'GET', status: 404 }, // Define general conditions that should cause failure { status: { from: 500, to: 599 } }, // All 5xx server errors { url: /critical-api/, status: 400 } // Bad requests to critical-api ] }; const pluginInstance = failOnNetworkError(initialConfig); const { getConfig, setConfig, waitForRequests } = pluginInstance; // Optionally expose methods as Cypress commands for use in tests Cypress.Commands.addAll({ getConfigRequests: () => { return cy.wrap(getConfig().requests); }, setConfigRequests: (requests: (string | Request)[]) => { setConfig({ ...getConfig(), requests }); }, waitForAllNetworkRequests: (timeout?: number) => waitForRequests(timeout) }); // cypress/e2e/my-test.cy.ts describe('Network Error Handling', () => { it('should pass if no configured network errors occur', () => { // Example: simulate a successful page load with no network issues cy.intercept('GET', '/data', { statusCode: 200, body: { message: 'success' } }).as('getData'); cy.visit('http://localhost:3000'); cy.get('body').contains('Welcome'); cy.waitForAllNetworkRequests(); }); it('should fail a test if a configured network error happens', () => { // This test would fail because a 500 error for /api/critical-data is configured to fail cy.intercept('GET', '/api/critical-data', { statusCode: 500, body: { error: 'server fault' } }).as('criticalError'); cy.visit('http://localhost:3000/dashboard'); cy.wait('@criticalError'); // The plugin will automatically fail the test here due to the 500 status code. // No explicit assertion needed for the failure condition. }); it('should dynamically update config for a specific test', () => { // Temporarily ignore a 404 error for this specific test cy.setConfigRequests(['/api/missing-resource', ...initialConfig.requests]); cy.intercept('GET', '/api/missing-resource', { statusCode: 404 }).as('missing'); cy.visit('http://localhost:3000/product/123'); cy.wait('@missing'); cy.waitForAllNetworkRequests(); // This test would now pass because the 404 to /api/missing-resource is explicitly ignored. }); });
Debug
Known issues
gotchaThe plugin's configuration for network errors is applied globally across all tests once initialized. Be mindful when running tests that intentionally trigger errors or specific status codes. If you need different error handling for individual tests, use `setConfigRequests` within `beforeEach` or specific `it` blocks, remembering that the config resets between tests.
fix
Initialize the plugin once in `cypress/support/e2e.ts` with a base configuration. Use `cy.setConfigRequests()` in `beforeEach` or `it` blocks to modify the behavior for specific tests or test suites, always considering the default configuration is restored between tests.
affects: >=1.0.0
gotchaThis plugin focuses on observing network requests. For errors logged to the browser's console (e.g., JavaScript runtime errors, console.error calls), you would need to use a separate plugin like `cypress-fail-on-console-error`. Combining both might be necessary for comprehensive error catching.
fix
If you need to catch both network and console errors, consider installing `cypress-fail-on-console-error` alongside this plugin and configuring both in your Cypress setup.
affects: >=1.0.0
breakingWhile not explicitly breaking the plugin's API, frequent updates to Cypress (as seen in release notes) mean users should ensure their `cypress-fail-on-network-error` version is compatible with their installed Cypress version to avoid unexpected behavior or integration issues.
fix
Always keep Cypress and its related plugins updated. If issues arise after a Cypress upgrade, check the `cypress-fail-on-network-error` GitHub repository for compatibility notes or update to the latest version of the plugin.
affects: >=1.0.0
gotchaThe `waitForRequests()` utility has a default timeout of 10000 ms. If your application or specific network requests are consistently slower than this, `waitForRequests()` will continue test execution without throwing an exception, potentially leading to race conditions where subsequent assertions run before all relevant requests have completed.
fix
Adjust the timeout for `waitForRequests(timeoutInMs)` if your application commonly has slower network activity. For critical requests, use `cy.wait('@alias')` for explicit waiting on specific intercepted requests.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: failOnNetworkError is not a function
Attempting to import the plugin using CommonJS `require()` syntax in a project that is configured for ESM, or directly calling `require()` on a module that is primarily ESM.
fix
Ensure you are using ESM `import failOnNetworkError from 'cypress-fail-on-network-error';` in your `cypress/support/e2e.js` (or `.ts`) file, and that your Cypress project's configuration (e.g., `cypress.config.js`) supports ESM.
Cypress command 'setConfigRequests' (or similar) failed because it is not a function.
The Cypress commands for `getConfigRequests`, `setConfigRequests`, or `waitForAllNetworkRequests` (or whatever custom names you chose) were not properly added to `Cypress.Commands.addAll` in your support file (`cypress/support/e2e.js` or `.ts`).
fix
Verify that your `cypress/support/e2e.js` (or `.ts`) file correctly initializes the plugin and exposes its methods as Cypress commands using `Cypress.Commands.addAll`. Reload Cypress after making changes to the support file.
Test passes unexpectedly, despite observing network errors in dev tools.
The `requests` configuration in `failOnNetworkError` is either too broad (excluding too much) or too specific (not matching the actual error-inducing requests). URLs, methods, or status codes might not precisely align with the network calls being made by your application under test.
fix
Carefully review your `requests` array configuration. Use browser developer tools or Cypress's `cy.intercept` to log and inspect the exact URLs, methods, and status codes of the network requests. Adjust your `requests` patterns (strings, RegExps) and conditions accordingly.
Upgrade
Version history
1.0.5latest on npm
Audit
Dependencies
cypressrequiredThis is a Cypress plugin and requires Cypress to function.
Agent activity
4 hits · last 30 days
node
4
Resources