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
5
votes
1 answer

How to add type definitions for module in subdirectory?

I am trying to mock requests made with axios using nock. This integration has some issues historically, but the workaround described in this issue works. My issue occurs when I introduce Typescript into the mix. While axios comes with type…
Chathan Driehuys
  • 1,173
  • 3
  • 14
  • 28
5
votes
1 answer

How to build nock regex for dynamic urls

How do i build nock configuration for the following types of urls http://example.com/harry/potter?param1=value1 and http://example.com/harry/ I have two type of urls first is where i can have query params altough the base url is fixed.…
Bipul Jain
  • 4,523
  • 3
  • 23
  • 26
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
  • 9,441
  • 16
  • 72
  • 123
5
votes
1 answer

Nock not working with axios get at actions async test

I am trying to test my async actions at redux but I am not getting it. I am using nock and axios, so I am trying to receive a responde data from axios get to test my actions: describe('Async Actions', () => { afterEach(() => { …
Alessander França
  • 2,697
  • 2
  • 29
  • 52
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,129
  • 8
  • 44
  • 63
4
votes
2 answers

How do I ignore certain domains from being records with nock.back?

I am using nock.back as part of mocha tests in my project. Even though I have setup nock.enableNetConnect, nock.back still records outgoing calls to the domain. const nock = require('nock') const path = require('path') nock.back.fixtures =…
akshah123
  • 708
  • 1
  • 5
  • 10
4
votes
1 answer

Code coverage concern on promise/asynchronous unit testing using nockjs and jest

I have written a simple unit test for API call using NockJS and Jest for a react application as bellow: AjaxService.js export const AjaxService = { post: (url, data, headers) => { return axios({ method: "POST", …
Suraj A J
  • 359
  • 4
  • 19
4
votes
1 answer

jest mock a property that is an object and a function at the same time

I am trying to mock a property of an object that acts as an object and as a function at the same time. Here's the code: index.js const nock = require('nock'); async function myFunc() { nock.back.setMode('param1'); const { nockDone } = await…
Joan Vilà
  • 265
  • 5
  • 9
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
  • 33,489
  • 95
  • 256
  • 444
4
votes
2 answers

Different response with same scope & path with different auth header with Nock, possible?

I'm using 3rd party API which has /places endpoint which returns the information of the places the authenticated user has permissions as JSON. That API is using JWT as authentication, the token is placed in X-Auth-Token header. My project has a…
Masza
  • 331
  • 1
  • 3
  • 12
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
  • 151
  • 2
  • 12
4
votes
3 answers

Nock: No match for request

My nock call looks like as below app_url='myshop.app.com' result = nock(app_url, { reqheaders: { "content-type": "application/json", 'authorization': 'Basic Auth' } }) …
user269867
  • 3,266
  • 9
  • 45
  • 65
4
votes
1 answer

Providing sessions when testing middleware in Express

I am attempting to test middleware functions in my Express application using supertest and nock, but am having issues where the routes I have setup are checked by an earlier piece of middleware to ensure a session property exists on the incoming req…
mindparse
  • 6,115
  • 27
  • 90
  • 191
4
votes
1 answer

Intercept all requests with different verbs in nock

This is how one of my intercept functions looks like now: interceptWithError() { nock(baseUrl) .get(/.*/) .replyWithError(500); nock(baseUrl) .put(/.*/) .replyWithError(500); nock(baseUrl) .post(/.*/) …
Yalda
  • 680
  • 1
  • 18
  • 39
4
votes
3 answers

redux - how use nock to test async action

I follow the basic exmaple of redux.org to test async action action.js my code is like this: import axios from 'axios' export function getGoodDataStart(){ return{ type: "GOOD_DATA_START" } } export function…
angry kiwi
  • 10,730
  • 26
  • 115
  • 161
1 2
3
18 19