question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nock doesn't appear to be intercepting middleware requests?

See original GitHub issue

What is the expected behavior? Nock successfully finds request inside of middleware.

What is the actual behavior? I have a piece of middleware that performs authentication with a third-party service via request. I do this request with superagent. Obviously I want to mock this in my tests since it slows them down quite a lot and also is dependant on the third-parties server.

When using nock, it doesn’t seem to find the request at all. I even tried using the recorder and it only picks up the actual requests of my local endpoints. (Although it uses an unfamiliar IP and port?).

How to reproduce the issue (Don’t really wanna sign up to a service so will just post my code below.)

Auth Middleware

export default async (req, res, next) => {
      const user = await superagent
      .get(`https://domain.com/session/`)
      .query({ session })
      .set('Api-Key', '1234');
}
nock('https://domain.com/session/')
  .persist()
  .get('/session/')
  .reply(200, {
    success: true,
    username: 'testuser',
  })
  .log(console.log);

Versions

Software Version(s)
Nock 10.0.6
Node 11.7.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gr2mcommented, Jan 25, 2019

I run into this a lot myself, definitely something we want to improve in future, but we first need to get some chore work done.

Glad you got it working!

1reaction
harshthakur92commented, Jan 24, 2019

I am using Same URL in different test file, Also added persist(), still i am getting error of “Expected false to be truthy” please help its irritating.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nock not intercepting http requests - Stack Overflow
Nock works by overriding Node's http.request function. Also, it overrides http.ClientRequest too to cover for modules that use it directly.
Read more >
nock reset between tests | The AI Search Engine You Control
This package uses nock for testing, which is a library that intercepts HTTP requests and allows for easy mocking of responses to quickly...
Read more >
nock - Devpost
Nock is an HTTP mocking and expectations library for Node.js ... It will intercept an HTTP GET request to '/users/1' and reply with...
Read more >
node-request-interceptor | Yarn - Package Manager
Low-level HTTP/HTTPS/XHR request interception library for NodeJS. Intercepts any requests issued by: http.get / http.request; https.get / https.
Read more >
Mocking External HTTP Requests in Node Tests with Nock
json file in your working directory that should look like this, depending on how you answered the prompts. { "name": "nock-test", "version": ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found