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
29
votes
4 answers

Error: Nock: No match for request

I am receiving following error { error: { Error: Nock: No match for request { "method": "GET", "url": "http://localhost:3000/admin/orders/30075889/transactions.json", "headers": { "content-type": "application/json", …
user269867
  • 3,266
  • 9
  • 45
  • 65
25
votes
4 answers

How to test error in request with Nock?

I want to test the error in a request return. I'm using nock in my tests, how can I force Nock to provoke an error? I want to achieve 100% test coverage and need to test err branch for that request('/foo', function(err, res) { if(err)…
coolxeo
  • 553
  • 1
  • 4
  • 10
19
votes
5 answers

Relative URL as hostname in Nock

I need to mock client side HTTP requests. I'm using isomorphic-fetch in the client side and I'm using mocha and nock for testing and mocking. All my client requests are based on relative path. Due to this I'm unable to provide host name for the…
Pranesh Ravi
  • 18,642
  • 9
  • 46
  • 70
16
votes
5 answers

nock is not intercepting my request

I'm trying to create some basic tests using karma server and nock. It seems like nock is not intercepting my requests at all, does anyone have idea? I can't figure out what is missing. I still getting real data. nock('https://api.github.com/users/'…
nico.amabile
  • 465
  • 1
  • 3
  • 12
16
votes
1 answer

how can superagent and nock work together?

In node.js, I have trouble making superagent and nock work together. If I use request instead of superagent, it works perfectly. Here is a simple example where superagent fails to report the mocked data: var agent = require('superagent'); var nock =…
xa4
  • 195
  • 1
  • 7
14
votes
2 answers

mocking server for SSR react app e2e tests with cypress.io

I'm building a single page web application (SPA) with server side rendering (SSR). We have a node backend API which is called both from the node server during SSR and from the browser after initial rendering. I want to write e2e tests that…
sylvain
  • 1,951
  • 2
  • 19
  • 34
12
votes
2 answers

Testing with JEST and nock causing 'Cross origin null forbidden'

I'm trying to test my service with JEST and mocking endpoint with nock. Service looks like this export async function get(id) { const params = { mode: 'cors', headers: { 'Accept': 'application/json', 'Content-Type':…
MarJano
  • 1,257
  • 2
  • 18
  • 39
11
votes
4 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 t
  • 209
  • 1
  • 2
  • 8
10
votes
1 answer

Nock not working for multiple tests running together

I am using nock library to stub my http calls. Different test files require('nock') and do their stubbing. If each test is run separately, all is passing. But if all tests run together, later tests fail because instead of nock, actual request was…
Rash
  • 7,677
  • 1
  • 53
  • 74
10
votes
2 answers

how to test react-saga axios post

I am learning how to test and using some examples as a guideline I am trying to mock a login post. The example used fetch for the http call but I using axios. This is the error I am getting Timeout - Async callback was not invoked within timeout…
texas697
  • 5,609
  • 16
  • 65
  • 131
10
votes
2 answers

Testing axios calls with Sinon, with redux and Karma

Hello in the redux documentation for testing they have have this example to test api calls: import configureMockStore from 'redux-mock-store' import thunk from 'redux-thunk' import * as actions from '../../actions/counter' import * as types from…
Bobby
  • 243
  • 4
  • 13
9
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
  • 2,340
  • 1
  • 24
  • 39
8
votes
1 answer

Nock.js: how do I check for the existence of a header?

I am using Nock with Mocha, and want to check that certain headers exist on a request. I don't care about the other headers, and I don't care about the specific content of the headers whose existence I'm checking for. Is there an easy way to do…
Boris K
  • 3,442
  • 9
  • 48
  • 87
8
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
  • 1,372
  • 1
  • 11
  • 18
8
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
  • 626
  • 1
  • 5
  • 16
1
2 3
18 19