Questions tagged [nock]

Nock is an HTTP mocking and expectations library for Node.js

Nock is an HTTP mocking and expectations library for Node.js

Nock can be used to test modules that perform HTTP requests in isolation.

For instance, if a module performs HTTP requests to a CouchDB server or makes HTTP requests to the Amazon API, you can test that module in isolation.

This does NOT work with Browserify, only node.js

Documentation

252 questions
6
votes
2 answers

How to see if nock is matching the request or not?

How to see if nock is matching the request or not? Is there a way one can log information on console regarding nock is matching or not to the requests being made?
pooja
  • 119
  • 1
  • 7
6
votes
1 answer

Nightwatch Mock HTTP Requests

I try mocking HTTP requests with nock and other libs like sinonjs but without success. import nock from "nock" const URL = "http://localhost:8080/" const SIGN_IN_PATH = "/fake/users/sign_in.json" export const signInRequest = (status, payload =…
Bruno Quaresma
  • 5,231
  • 4
  • 25
  • 41
6
votes
2 answers

Nock + multipart form data = No match for request

I have a problem with testing my node application using using Nock. I record all requests via nock.recorder.rec, but among them there multipart request. I use form-data. This module put the boundary to request body, when i use function form.append.…
psixdev
  • 131
  • 1
  • 6
5
votes
1 answer

npm module function interception. specifically base module 'dns'

import DNS from 'dns' DNS.resolveTxt('test-website.com', (err, addresses) => { console.log(err, addresses); }) Above would be an example usage of how the node resolves a web address. I want to build a nock library for 'dns', and intercept…
gatsbyz
  • 891
  • 1
  • 9
  • 23
5
votes
0 answers

How to make assertions on request body axios.js in Node.js doing real HTTP calls?

I am making request with axios.js library and part of my integration test suite are expectations on data send in body of POST request, done by axios, to see if it's ok. During development I'he used nock.js and put expectations inside .post nock…
zmii
  • 3,268
  • 2
  • 29
  • 53
5
votes
1 answer

Electron: mocking out network requests for tests

I'd like to write unit tests for some of my network logic in an Electron app, and need to mock out the network endpoints. In the past, I've used nock for mocking out HTTP requests in Node. However, my Electron code uses the electron.net module for…
Scott Rippey
  • 14,881
  • 5
  • 68
  • 83
5
votes
0 answers

Nock with post request and file attachment

Having trouble using Nock.js to mock an HTTP Post Request with csv file attachment. With superagent this is possible: authenticatedSession .post('/foo') .set('Content-Type', 'multipart/form-data') .attach('csv', barCsv) …
5
votes
1 answer

How to ignore headers when running a mocked request with nock?

I need to mock a request using nock module, which is issued by a module that adds extra headers (x-md5-checksum). And the request does not match because of those headers. How do I force nock to ignore this header and match the request…
moltar
  • 475
  • 1
  • 4
  • 13
5
votes
3 answers

How to add params with GET request using nock.js

I am trying to test if my API routes are working using nock.js to mock url requests. My routing file routes according to the following logic: app.get('/api/study/load/:id', abc.loadStudy ); 'loadStudy' method in 'abc.js' is used to handle the…
Navjot Singh
  • 538
  • 4
  • 16
5
votes
1 answer

Nock intercepts request but returns empty object

I'm using mocha as the testing framework and I'm trying to mock up a DELETE request that uses fetch against an end point that returns HTTP status code 204. Here is the test code: it('should logout user', (done) => { nock() …
rfc1484
  • 8,473
  • 15
  • 58
  • 111
5
votes
1 answer

Nock - how to mock binary response

I'm writing code interacting with PayPal classic API. The first part of this interaction is to send request to PayPal and get a token from them. For that I use simple https request: function makePayPalRequestForToken(options, callback) { var…
Jakub
  • 3,049
  • 8
  • 42
  • 63
4
votes
1 answer

How to do subsequent calls on same url via Nock having different status code

Problem: I want to mock a situation in which on the same http call I get different results. Specifically, the first time it fails. To some extent this is similar to Sinon capability of stub.onFirstCall(), stub.onSecondCall() Expectation: I expected…
Dudi
  • 1,870
  • 1
  • 20
  • 35
4
votes
1 answer

React API test with Nock failing with "Error : Nock : No match for request"

Here's the Express Route code that is working fine in back and frontend. // Edit/update by vessel_type by_id - Working router.put("/:id", (req, res, next) => { Vessel_Type.findByIdAndUpdate( req.params.id, req.body, { new: true }, …
Rohan_Paul
  • 1,207
  • 21
  • 30
4
votes
0 answers

How to cover unit test branch in object property as function

Hello I am writing unit test case of my function which is like const Services = { test: async (token) => { .. ... } } I have written unit test of this function, report says test covered all lines of this function but async (token) =>…
N Sharma
  • 28,073
  • 81
  • 228
  • 405
4
votes
1 answer

Mocking with Nock, mock only a specific route with the same host

I am using Nock ( https://github.com/node-nock/nock ) to mock an underly service called by an endpoint that I need to test in my application. In the implementation of the endpoint, I am calling this underly service multiple time like that…
Jbeat
  • 101
  • 1
  • 11
1
2
3
16 17