interceptors: TypeError: this.agent.destroy is not a function

I am using Vitest to run the test, when I intercept the request.js requests, the program issues an error message

vitest: 0.25.2 request.js: 2.28.2 msw: 0.48.3

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
TypeError: this.agent.destroy is not a function
 ❯ NodeClientRequest.terminate node_modules/msw/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/NodeClientRequest.ts:361:16
 ❯ NodeClientRequest.respondWith node_modules/msw/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/NodeClientRequest.ts:353:10
 ❯ node_modules/msw/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/NodeClientRequest.ts:164:14
 ❯ processTicksAndRejections node:internal/process/task_queues:95:5
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (15 by maintainers)

Commits related to this issue

Most upvoted comments

@markfranco, thanks for letting me know. The versions are correct, MSW is not yet on the latest Interceptors because those work via the Fetch API already. I may try backporting this into the latest compatible release.

@kettanaito It’s apparently an issue with OctoKit. Taking a gander at their source code, I found this:

  return fetch(
    requestOptions.url,
    Object.assign(
      {
        method: requestOptions.method,
        body: requestOptions.body,
        headers: requestOptions.headers as HeadersInit,
        redirect: requestOptions.redirect,
      },
      // `requestOptions.request.agent` type is incompatible
      // see https://github.com/octokit/types.ts/pull/264
      requestOptions.request as any
    )
  )
...

@kettanaito it was a premature celebration after all… the interceptor is apparently not doing anything at all in v19, which would explain it not throwing either.

edit: It’s happening both in 17.9.1 and 16.19.0

@kettanaito I am experiencing this same error, not related to request.js (reported it over at the msw repo in https://github.com/mswjs/msw/issues/1515) while trying to intercept OctoKit requests.

I tried dropping some random console calls for debugging and from what I can see, it appears this.agent is undefined at that point, for some reason.