Attaching Hooks to Tests and Fixtures

To attach a hook to a test or fixture, use the fixture.requestHooks and test.requestHooks methods. A hook attached to a fixture handles requests from all tests in the fixture.


Hooks attached to the fixture run first when you attach request hooks to the fixture and test.

You can use the t.addRequestHooks and t.removeRequestHooks methods to attach and detach hooks during the test.

Parameter Type Description
hooks RequestHook subclass A RequestLogger, RequestMock or custom user-defined hook.

The fixture.requestHooks, test.requestHooks, t.addRequestHooks and t.removeRequestHooks methods use the rest operator which allows you to pass multiple hooks as parameters or arrays of hooks.

import { RequestLogger, RequestMock } from 'testcafe';

const logger = RequestLogger('');
const mock   = RequestMock()
    .respond({ data: 'value' });

fixture `My fixture`

    ('My test', async t => {
    await t
         .expect(logger.count(() => true)).eql(1)
         .expect(logger.count(() => true)).eql(1)
         .expect(logger.count(() => true)).eql(2);