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()
.onRequestTo('https://api.mycorp.com/users/id/135865')
.respond({
name: 'John Hearts',
position: 'CTO',
}, 200, { 'access-control-allow-origin': '*' })
.onRequestTo(/internal.mycorp.com/)
.respond(null, 404);
fixture`RequestMock`
.page`https://mycorp.com`
.requestHooks(mock);
test('Should mock requests', async t => {
const user = await ClientFunction(() => fetch('https://api.mycorp.com/users/id/135865').then(res => res.json()))();
await t
.expect(user).eql({ name: 'John Hearts', position: 'CTO' })
.navigateTo('https://internal.mycorp.com');
});