Chandler Moisen

TestCafe Request Hooks

May 13, 2020

Today I learned about intercepting HTTP requests using TestCafe request hooks.

I have recently incorporated writing automated end-to-end tests into my workflow using TestCafe. When writing such tests (or any tests for that matter) you might find yourself in a situation where you need to mock an external service. TestCafe allows mocking of services via its RequestMock class.

Setting up mocks to run before a series of tests is straight forward. For example, if you are testing a UI that is only accessible to authorized users, you can mock out the authorization request as follows:

import { RequestMock, Selector } from 'testcafe'

const authMock = RequestMock()
    token: 'abc123',

fixtures('Profile Page')

© 2013 - 2022
Built with Gatsby