BedrockStreaming/superagent-mock

v2.0.1: TypeError: first argument must be a string or Buffer

Closed this issue · 2 comments

After upgrading superagent-mock from 1.12.0 -> 2.x, all my unit tests which has a request.post.send

request.post(login_url)
  .send({
     username: username,
     token: token
  }).then((res) =>
     // assertions
  )

are failing with the following error

TypeError: first argument must be a string or Buffer 

at ClientRequest.OutgoingMessage.end (_http_outgoing.js:542:11)
      at Request.end (/opt/default/lib/node_modules/superagent/lib/node/index.js:873:9)
      at Request.end (/opt/default/lib/node_modules/superagent-mock/lib/superagent-mock.js:153:21)
      at /opt/default/lib/node_modules/superagent/lib/request-base.js:72:12
      at Request.then (/opt/default/lib/node_modules/superagent/lib/request-base.js:71:31)

The same code works fine if I revert to 1.12.0. I am using superagent v2.3.0 and superagent-mock v2.0.1.

If a real http call is made by superagent, it means no pattern match your login_url anymore. What is your pattern and your post url ?

@fdubost @D34THWINGS @flepretre, we may mock then and catch superagent/RequestBase methods

Was able to finally get this fixed in my project. The issue was:

I had 2 unit test files testA and testB. testA was encountering the issue I reported above which did not use superagent-mock at all. However, testB was calling superagent-mock but did not do an unset() in the teardown. I used MOCHA with --grep option to run only the tests of testA.

superagent-mock in testB monkey patched superagent which caused the tests fail in testA

Closing this bug for now. Feel free to open if you see this as a bug with superagent-mock.