Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Since 0.21.2, nock-based tests of client that uses axios fail #4036

Closed
filmaj opened this issue Sep 8, 2021 · 10 comments
Closed

Since 0.21.2, nock-based tests of client that uses axios fail #4036

filmaj opened this issue Sep 8, 2021 · 10 comments

Comments

@filmaj
Copy link

filmaj commented Sep 8, 2021

Hi 👋

Before diving into the issue, just want to say: thanks for creating, maintaining and supporting this great library! Much appreciated ❤️

Describe the bug

Since axios v0.21.2, tests for Slack's web-api npm package have started failing (see e.g. this test run).

This package uses nock for testing, which is a library that intercepts HTTP requests and allows for easy mocking of responses to quickly test things like API client libraries that use axios under the hood. nock-based tests for certain specific API methods of our library started failing in the past few days.

After some digging, we were able to determine that something changed between axios v0.21.1 and v0.21.2. v0.21.1 works as expected, however v0.21.2 and newer (including .3 and .4) yields failures. It looks like newer axios versions' requests don't get intercepted by nock anymore 🤔

To Reproduce

I have created a reproduction case as a gist: https://gist.github.com/filmaj/f5097da82ecc9d429b983ee5561b8839

Clone the above, cd into the directory, run npm install and then npm test. Tests should pass. Great! Now for the not-so-great part: edit package.json and bump up the axios dependency from 0.21.1 to e.g. 0.21.2 or newer. Run npm install again and then npm test. Womp womp, failures.

Expected behavior

Using the above repro case / gist, this is what output looks like (the debug output is from both the Slack web-api debug logs as well as a subset of nock's debug output) when using axios v0.21.1:

➜ npm test

> axios-repro@1.0.0 test /Users/fmaj/src/axios-repro
> DEBUG=nock.scope* mocha test.js

[DEBUG]  web-api:WebClient:0 initialized


  has all admin.usergroups.* APIs
[DEBUG]  web-api:WebClient:0 apiCall('admin.usergroups.addChannels') start
[DEBUG]  web-api:WebClient:0 will perform http request
  nock.scope:slack.com reply.headers: {} +0ms
  nock.scope:slack.com reply.rawHeaders: [] +1ms
  nock.scope:slack.com attempting match {"path":"/api/admin.usergroups.addChannels","method":"POST","headers":{"accept":"application/json, text/plain, */*","user-agent":"@slack:web-api/6.4.0 node/16.4.2 darwin/20.6.0","content-type":"application/x-www-form-urlencoded","content-length":75},"agents":{},"hostname":"slack.com","port":443,"proto":"https","host":"slack.com:443","protocol":"https:"}, body = "token=xoxb-faketoken&team_id=T123&usergroup_id=S123&channel_ids=C123%2CC234" +4ms
  nock.scope:slack.com query matching skipped +0ms
  nock.scope:slack.com matching https://slack.com:443/api/admin.usergroups.addChannels to POST https://slack.com:443/api/admin.usergroups.addChannels: true +0ms
  nock.scope:slack.com interceptor identified, starting mocking +0ms
  nock.scope:slack.com response.rawHeaders: [] +0ms
  nock.scope:slack.com emitting response +3ms
[DEBUG]  web-api:WebClient:0 http response received
    ✔ can call admin.usergroups.addChannels with a string "channel_ids"


  1 passing (18ms)

Actual behavior

Using the above repro case / gist, this is what output looks like (the debug output is from both the Slack web-api debug logs as well as a subset of nock's debug output) when using axios v0.21.2:

➜ npm test

> axios-repro@1.0.0 test /Users/fmaj/src/axios-repro
> DEBUG=nock.scope* mocha test.js

[DEBUG]  web-api:WebClient:0 initialized


  has all admin.usergroups.* APIs
[DEBUG]  web-api:WebClient:0 apiCall('admin.usergroups.addChannels') start
[DEBUG]  web-api:WebClient:0 will perform http request
  nock.scope:slack.com reply.headers: {} +0ms
  nock.scope:slack.com reply.rawHeaders: [] +1ms
[DEBUG]  web-api:WebClient:0 http response received
    1) can call admin.usergroups.addChannels with a string "channel_ids"


  0 passing (2s)
  1 failing

  1) has all admin.usergroups.* APIs
       can call admin.usergroups.addChannels with a string "channel_ids":
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/fmaj/src/axios-repro/test.js)
      at listOnTimeout (node:internal/timers:557:17)
      at processTimers (node:internal/timers:500:7)



npm ERR! Test failed.  See above for more details.

Environment

  • Axios Version: 0.21.2 or newer
  • Node.js Versions: reproduced on 12, 14 and 16
  • OS: macOS 11.5.2

Additional context/Screenshots

Leaving this section here as empty because apparently the issue will close if I remove this section?

@jasonsaayman
Copy link
Member

@filmaj will have a look soonest :)

filmaj added a commit to slackapi/node-slack-sdk that referenced this issue Sep 8, 2021
@gfortaine
Copy link
Contributor

gfortaine commented Sep 9, 2021

It seems to come from #2702 cc @SashaKoro @nerdbeere

macsj200 pushed a commit to lifeomic/alpha that referenced this issue Sep 9, 2021
There is a dependabot PR in forum-service to bump axios to `0.21.2`. This PR is failing unit tests for some reason. I am hoping that upgrading past `0.21.2` to the latest `0.21.4` resolves these issues. Unfortunately the type signatures for the latest version of axios and the version of axios included in `@lifeomic/alpha` don't match.

This PR bumps axios to the latest version.

See:
https://github.com/lifeomic/forum-service/pull/171
https://github.com/lifeomic/life-extend/pull/1690
axios/axios#4036
@landerson-gc
Copy link

landerson-gc commented Sep 11, 2021

@jasonsaayman @filmaj
I am experiencing similar test failures when using sinon/nise FakeServer and FakeTimers in Jest.
see: https://github.com/axios/axios/pull/3688/files#r706543576

@BeeeQueue
Copy link

Can confirm this problem, in my testing it occurred in all versions >=0.21.0

@xmariopereira
Copy link

So, as you all maybe know. There is a high-security issue in all versions below and including 0.21.1.

In some packages.json can't be upgraded to 0.21.2 because the tests are failing.

From a security perspective, (nobody wants CISO breathing on the developer's neck). Maybe this should have a higher priority. :)

@filmaj
Copy link
Author

filmaj commented Nov 2, 2021

I'm starting to dig into this issue a bit more, I haven't made much progress yet but for the record this issue still exists in 0.24.0.

@guyskk
Copy link

guyskk commented Dec 25, 2021

Just verified the issue is caused by #2609, revert the PR then problem disappear:

git reset --hard 62d6256
git revert 62d6256
npm run build
npm pack

# verify by install local package to my project
npm install ../axios/axios-0.21.1.tgz

@guyskk
Copy link

guyskk commented Dec 25, 2021

@guyskk
Copy link

guyskk commented Dec 25, 2021

Sorry for the bother, the bug already fixed in #4013, upgrade to newer version(0.21.4) resolved my problem. 😅😅

@jasonsaayman
Copy link
Member

Closing as this is solved as mentioned above

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

No branches or pull requests

7 participants