Nock doesn't appear to be intercepting middleware requests?
See original GitHub issueWhat 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:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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!
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.