Cypress

Cypress Integration

Q: How do I integrate Stoobly with Cypress tests?

A: Create a Stoobly instance and Cypress interceptor, then call apply() in your beforeEach hook.

Example:

import Stoobly from 'stoobly';
import { RecordPolicy, RecordOrder, RecordStrategy } from 'stoobly/constants';

const stoobly = new Stoobly();
const interceptor = stoobly.cypressInterceptor({
  scenarioKey: '<SCENARIO-KEY>',
  urls: [new RegExp('https://api.example.com/.*')],
});

describe('My Tests', () => {
  beforeEach(() => {
    // Apply interceptor in beforeEach
    interceptor.apply();
  });

  it('can fetch data', () => {
    cy.visit('https://example.com');
    // Your test code here
  });
});

Q: Why must I call apply() in beforeEach for Cypress?

A: Cypress automatically clears all intercepts before every test, so you must reapply the Stoobly interceptor in beforeEach.

Example:

Q: How do I record requests in Cypress tests?

A: Use applyRecord() instead of apply() to enable recording mode.

Example:

Q: What's the warning about synchronous requests in Cypress?

A: Synchronous XMLHttpRequest calls will cause Cypress to hang. Avoid using synchronous requests when Stoobly interceptor is active.

Example:

Last updated