Stub

General

// Definition

A test double that returns canned responses to method calls, replacing real behaviour without recording or asserting on calls.

// Code Example

SinonStub a dependency for predictable returns
TypeScript
import sinon from 'sinon';
import * as api from './api';

const stub = sinon.stub(api, 'fetchUser').resolves({
  id: 1,
  name: 'Ada',
});

const user = await api.fetchUser(1);
expect(user.name).toBe('Ada');
stub.restore();

// Related terms

Learn more · Cypress with TypeScript

Chapter 4 · Lesson 2: Stubbing API Responses