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

281 questions
8
votes
3 answers

Nock is not intercepting API call in Redux test

I'm trying to test an api call in a redux app. The code pretty much follows the pattern outlined in the Async Action Creators section of the redux docs: http://redux.js.org/docs/recipes/WritingTests.html The gist of it is that you use…
Mike Rifgin
  • 10,409
  • 21
  • 75
  • 111
8
votes
1 answer

Node.js Nock simulate request timeout and subsequent success

I'm trying to simulate service request timeouts to test the node requestretry module, which allows you to specify a request max # of attempted retries and retry delay. In order to test this, I need to use nock to simulate a timeout for the first X…
ejfrancis
  • 2,925
  • 4
  • 26
  • 42
8
votes
4 answers

How to decode nock recorded response

How to ad-hoc decode/uncompress the output produced by the nock recorder so we can see the response as text? I guess we do not understand if the response is gzipped and/or encoded The object works find when we load it into nock, and our tests are…
donnie_armstrong
  • 1,353
  • 2
  • 10
  • 8
7
votes
1 answer

How to return object instead of string for response with nock?

When I stub request with nock it returns String result instead of Object even with 'Content-Type': 'application/json': var response = { success: true, statusCode: 200, body: { "status": "OK", "id": "05056b27b82", …
zishe
  • 10,665
  • 12
  • 64
  • 103
6
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
  • 4,123
  • 3
  • 40
  • 64
6
votes
2 answers

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
  • 15,614
  • 5
  • 70
  • 85
6
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) …
6
votes
0 answers

How to create request and response objects in node

I'm trying to mock request and response objects for my node/express handlers. I've tried a few mocking libraries and have run into issues with API compatibility, which has made them too unreliable for testing purposes. What I would like to do is…
Seán Hayes
  • 4,060
  • 4
  • 33
  • 48
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
  • 9,457
  • 7
  • 32
  • 50
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
  • 8
5
votes
1 answer

How to use nock in order to mock Firebase/Firestore in NodeJS?

I have a REST API which written in NodeJS. The REST API gets a file and information and then uploads the file to the drive and stores the information inside my Firebase/Firestore DB. I'm trying to write tests in order to test my REST API, so I need…
vesii
  • 2,760
  • 4
  • 25
  • 71
5
votes
1 answer

Mocking API calls with Detox and Nock

I'm trying to mock API calls from Detox tests and nothing seems to be working. Nock in theory would do exactly what I want but there when I run my tests with nock debugging it isn't seeing any of the requests being made from my app. I'm using axios…
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
  • 1,057
  • 1
  • 10
  • 26
5
votes
3 answers

Disable a single nock scope immediately to re-setup a mocked URL

Using nock, is there a way to disable a single nock scope? I've been struggling with some tests that set up nocks of the same URL as some other tests. They both run fine separately, but when run in the same mocha session one of them fails, because…
JHH
  • 8,567
  • 8
  • 47
  • 91
5
votes
2 answers

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,504
  • 3
  • 29
  • 36
1
2
3
18 19