'Testcafe request event requestOptions is undefined in custom request hook?

I'm trying to create a custom request hook that modifies the Referer header on my outbound requests for my Testcafe tests. I have followed the docs as well as examples found here, but I'm running into an issue where the requestOptions property, that I'm supposed to use to set the header, is undefined on the event that comes into the hook. I'm completely stumped at this point.

Here's the relevant code:

import * as h from "../TestHelper";
import { RequestHook } from "testcafe";

const CLIENT = "san-fran";

class SetCorrectRefererHeader extends RequestHook {
  constructor(requestFilterRules, responseEventOptions) {
    super(requestFilterRules, responseEventOptions);
  }

  async onRequest(e) {
    if (e.isAjax) {
      console.log("request event is", e);
      console.log("request options", e.requestOptions);
      console.log("requestInfo", e.requestInfo);
      console.log("_requestInfo", e._requestInfo);
      e.requestOptions.headers["Referer"] = `${h.getClientUrl(CLIENT)}`;
    }
  }

  async onResponse(e) {
    //console.log("response event is", e);
  }
}

const setCorrectReferer = new SetCorrectRefererHeader();

// using this test just to make sure hook works
test.requestHooks(setCorrectReferer)(`referer`, async t => {
  await t.navigateTo(`https://whatsmyreferer.com/`);

  await t.debug();
});

and this is what gets logged to my terminal from those logging statements when the ajax request is made:

{} request event is RequestEvent {
  requestFilterRule: RequestFilterRule {
    options: { url: /.*/, method: undefined, isAjax: undefined },
    id: 'HeKfTLWm3',
    isPredicate: false
  },
  _requestContext: null,
  _requestInfo: {
    requestId: 'if97H3RMU',
    userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36',
    url: 'https://www.google-analytics.com/j/collect?v=1&_v=j96&a=719088816&t=pageview&_s=1&dl=https%3A%2F%2Fwhatsmyreferer.com%2F&ul=en-us&de=UTF-8&dt=What%27s%20My%20Referer%20%3F&sd=24-bit&sr=3360x1890&vp=1200x1668&je=0&_u=IEBAAEABAAAAAC~&jid=661957536&gjid=1633557977&cid=2140452944.1649960915&tid=UA-36499947-1&_gid=1676613645.1649960915&_r=1&_slc=1&z=1607123390',
    method: 'post',
    isAjax: true,
    headers: {
      host: 'www.google-analytics.com',
      connection: 'keep-alive',
      'content-length': 0,
      'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36',
      'content-type': 'text/plain',
      accept: '*/*',
      origin: 'https://whatsmyreferer.com',
      'accept-encoding': 'gzip, deflate',
      'accept-language': 'en-US,en;q=0.9'
    },
    body: <Buffer >,
    sessionId: '5COBqXww8'
  },
  id: 'uUuQv4azM',
  setMock: [AsyncFunction (anonymous)]
}
{} request options undefined
{} requestInfo undefined
{} _requestInfo {
  requestId: 'if97H3RMU',
  userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36',
  url: 'https://www.google-analytics.com/j/collect?v=1&_v=j96&a=719088816&t=pageview&_s=1&dl=https%3A%2F%2Fwhatsmyreferer.com%2F&ul=en-us&de=UTF-8&dt=What%27s%20My%20Referer%20%3F&sd=24-bit&sr=3360x1890&vp=1200x1668&je=0&_u=IEBAAEABAAAAAC~&jid=661957536&gjid=1633557977&cid=2140452944.1649960915&tid=UA-36499947-1&_gid=1676613645.1649960915&_r=1&_slc=1&z=1607123390',
  method: 'post',
  isAjax: true,
  headers: {
    host: 'www.google-analytics.com',
    connection: 'keep-alive',
    'content-length': 0,
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36',
    'content-type': 'text/plain',
    accept: '*/*',
    origin: 'https://whatsmyreferer.com',
 ✖ referer

   1) undefinedAn unhandled error occurred in the "onRequest" method of the "SetCorrectRefererHeader" class:
      
      TypeError: Cannot read property 'headers' of undefined

      Browser: Chrome 100.0.4896.88 / macOS 10.15.7

I feel like I have to be missing something, but I can't figure out what...



Solution 1:[1]

I can't reproduce your case in Testcafe (version 1.18.5) and Chrome (version 100.0.4896.88). Make sure that you use the latest versions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aleksey