Skip to content

Conversation

codders
Copy link

@codders codders commented Aug 12, 2025

In node environments, the code in node/agent.js creates a proxy object around Agent so that it can be instantiated without using the new keyword. The equivalent code for the browser version (in client.js) defines request.agent as () => new Agent().

In supertest, the TestAgent attempts to subclass Agent with the call Object.setPrototypeOf(TestAgent.prototype, Agent.prototype);. This works in the node environment but not in the browser version, because in the browser version () => new Agent() is not an object and has an undefined prototype, leading to the error message Object prototype may only be an Object or null: undefined.

Wrap request.agent in a proxy object for the client/browser case so that supertest can also use the library in browser contexts without error.

Checklist

  • I have ensured my pull request is not behind the main or master branch of the original repository.
  • I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first.
  • I have written a commit message that passes commitlint linting.
  • I have ensured that my code changes pass linting tests.
  • I have ensured that my code changes pass unit tests.
  • I have described my pull request and the reasons for code changes along with context if necessary.

In node environments, the code in `node/agent.js` creates a proxy
object around `Agent` so that it can be instantiated without using
the `new` keyword. The equivalent code for the browser version
(in `client.js`) defines `request.agent` as `() => new Agent()`.

In supertest, the `TestAgent` attempts to subclass `Agent` with the
call `Object.setPrototypeOf(TestAgent.prototype, Agent.prototype);`.
This works in the node environment but not in the browser version,
because in the browser version `() => new Agent()` is not an object
and has an undefined prototype, leading to the error message
`Object prototype may only be an Object or null: undefined`.

Wrap `request.agent` in a proxy object for the client/browser case
so that supertest can also use the library in browser contexts
without error.
@codders
Copy link
Author

codders commented Aug 12, 2025

Fixes #1840

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant