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
21
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
  • 513
  • 1
  • 3
  • 10
18
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
  • 15,581
  • 6
  • 40
  • 61
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
4 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
  • 397
  • 2
  • 11
13
votes
3 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
  • 2,280
  • 6
  • 35
  • 57
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
  • 4,171
  • 16
  • 56
  • 115
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
  • 123
  • 1
  • 7
9
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':…
Marceli Janowski
  • 1,097
  • 1
  • 13
  • 36
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
  • 8,840
  • 18
  • 66
  • 108
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,465
  • 3
  • 22
  • 41
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,303
  • 2
  • 10
  • 8
7
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
  • 2,748
  • 5
  • 36
  • 68
7
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,781
  • 2
  • 17
  • 33
7
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
  • 6,184
  • 1
  • 42
  • 58
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,055
  • 12
  • 60
  • 101
1
2 3
16 17