RequestMock Class

A request mocker that intercepts requests to a web resource and emulates the response.

Use the RequestMock constructor to create a mocker.

Call the onRequestTo and respond methods in a chain to specify mock responses for every handled request.

To use the mocker during tests, attach it to a test or fixture.

import { ClientFunction, RequestMock } from 'testcafe';

const mock = RequestMock()
        name:     'John Hearts',
        position: 'CTO',
    }, 200, { 'access-control-allow-origin': '*' })
    .respond(null, 404);


test('Should mock requests', async t => {
    const user = await ClientFunction(() => fetch('').then(res => res.json()))();

    await t
        .expect(user).eql({ name: 'John Hearts', position: 'CTO' })