Skip to content
Latest stable: v0.8.0.

Cypress

Cypress injects Chaos Maker through custom commands. Register the Node-side task plus the browser-side support module once, then call cy.injectChaos() before cy.visit().

Terminal window
npm install @chaos-maker/core @chaos-maker/cypress

In cypress.config.ts, register the chaos:getUmdSource task so cy.injectChaos() can load the core UMD bundle at runtime:

import { defineConfig } from 'cypress';
import { registerChaosTasks } from '@chaos-maker/cypress/tasks';
export default defineConfig({
e2e: {
baseUrl: 'http://localhost:3000',
setupNodeEvents(on) {
registerChaosTasks(on);
},
},
});

Then in cypress/support/e2e.ts, register the browser-side commands:

import '@chaos-maker/cypress/support';

The fastest start is a named preset - drop a scenario into the config and run:

describe('checkout', () => {
it('works under degraded mobile network', () => {
cy.injectChaos({ presets: ['mobile-3g', 'checkout-degraded'], seed: 42 });
cy.visit('/checkout');
cy.get('[data-testid="checkout-form"]').should('be.visible');
});
});

See the Presets catalog for the full list. When several tests should share the same named scenario, wrap it into a scenario profile - the one built-in demo profile composes both presets above into a single name:

cy.injectChaos({ profile: 'mobile-checkout', seed: 42 });

Pass profileOverrides alongside profile to tune one parameter for this call without forking the profile. When you need precise control, drop down to explicit rules:

describe('orders', () => {
it('shows error state when the API fails', () => {
cy.injectChaos({
seed: 42,
network: {
failures: [{ urlPattern: '/api/orders', statusCode: 503, probability: 1 }],
},
});
cy.visit('/orders');
cy.get('[data-testid="error-state"]').should('be.visible');
cy.getChaosLog().should((log) => {
expect(log.some((event) => event.type === 'network:failure' && event.applied)).to.equal(true);
});
});
});

Log the replay seed only when a test fails:

import { formatSeedReproduction } from '@chaos-maker/cypress';
afterEach(function () {
if (this.currentTest?.state === 'failed') {
cy.getChaosSeed().then((seed) => {
console.error(formatSeedReproduction(seed));
});
}
});

Cypress command log entries include the same event types returned by cy.getChaosLog(), so test output and assertions stay aligned.