Mock

General

// Definition

A test double that records how it was called and lets the test assert on those interactions. Pre-programmed with expectations about which methods will be called and how.

// Code Example

CypressMock an API response with cy.intercept
TypeScript
// Force a known response and assert UI behaviour
cy.intercept('GET', '/api/tools', {
  statusCode: 200,
  body: [{ id: 'cypress', name: 'Cypress' }],
}).as('getTools');

cy.visit('/tools');
cy.wait('@getTools');
cy.contains('Cypress').should('be.visible');

// Related terms

Learn more · Cypress with TypeScript

Chapter 4 · Lesson 2: Stubbing API Responses