New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intercept requests with nock #305

Closed
batusai513 opened this Issue Apr 20, 2016 · 11 comments

Comments

Projects
None yet
10 participants
@batusai513

batusai513 commented Apr 20, 2016

Hi, i wanted to know if it is posible to intercept requests made by axios with nock for testing purposes, has anyone tried that?

thanks in advanced for your help

@chibicode

This comment has been minimized.

chibicode commented Apr 21, 2016

I wrote this on #175 but are you failing on a POST request from axios? I'm getting mismatch on nock because nock seems to be attempting to match OPTIONS method. #225

I'm using axios 0.10.0 and nock 8.0.0.

Here's my nock interception code:

nock('http://localhost:5000')
  .post(`/path`, data)
  .log(console.log)
  .reply(200, { id: 1 })

My axios initialization:

import axios from 'axios'

const instance = axios.create({
  baseURL: (process.env.NODE_ENV === 'production') ? 'https://foo.com/' : 'http://localhost:5000/',
  headers: {
    ...
  }
})

export default instance

Calling axios:

axios.post('/path', {})

Then I get Network Error on the test. log(console.log) on nock outputs this:

matching http://localhost:5000 to POST http://localhost:5000/foo: false 

When I run it with DEBUG=nock.* I get this:
https://gist.github.com/chibicode/39a17e2e7aa46899865b8b10379b26f4

I'm seeing a bunch of OPTIONS requests, maybe nock isn't correctly matching against it?

@batusai513

This comment has been minimized.

batusai513 commented May 3, 2016

@chibicode yeah, this is probably what is happening, have you been able to fix this?

@sathify

This comment has been minimized.

sathify commented May 6, 2016

Same problem here not able to test with nock.

@mzabriskie

This comment has been minimized.

Member

mzabriskie commented May 31, 2016

I don't have much experience with nock, but I have a couple suggestions for what it's worth.

  • If you want to avoid OPTIONS method and are running in node you can force the node adapter: axios.defaults.adatper = require('axios/lib/adapters/http');
  • Try using moxios for mocking your tests, which I created specifically for testing axios clients.
@mzabriskie

This comment has been minimized.

Member

mzabriskie commented Jul 16, 2016

Closing due to inactivity. Hoping that moxios is solving the problem for everyone :)

@mzabriskie mzabriskie closed this Jul 16, 2016

@ConAntonakos

This comment has been minimized.

ConAntonakos commented Sep 27, 2016

Edit: Oh, my. I just realized that the defaults.adapter = ... was misspelled. The Network Error disappeared at least!

Using:

  • axios 0.14.0
  • react 15.3.1
  • nock 8.0.0

I tried axios.defaults.adatper = require('axios/lib/adapters/http'); for the particular file I'm testing, but I'm still encountering an issue.

I'm also using nock to mock the API request, and testing an axios request in a React.js lifecycle method.

I'm probably using all of this incorrectly, but would love to read others' thoughts. 😃

This is the error I'm receiving:

{ [Error: Network Error]
      config:
       { transformRequest: { '0': [Function: transformRequest] },
         transformResponse: { '0': [Function: transformResponse] },
         headers: { Accept: 'application/json, text/plain, */*' },
         timeout: 0,
         xsrfCookieName: 'XSRF-TOKEN',
         xsrfHeaderName: 'X-XSRF-TOKEN',
         maxContentLength: -1,
         validateStatus: [Function: validateStatus],
         adatper: [Function: httpAdapter],
         method: 'get',
         params: { initial: true },
         url: 'http://localhost:8080/api/test,
         data: undefined },
      response: undefined }
@jordanpapaleo

This comment has been minimized.

jordanpapaleo commented Oct 22, 2016

After three days of 'nock'ing my head against the wall, I found this post and tried moxios. Worked first time. Thank you @mzabriskie for preventing me from retiring as a developer and becoming a barista ;)

@dyakimenko-p

This comment has been minimized.

dyakimenko-p commented Jan 12, 2017

Works for me:

import {expect} from 'chai'
import axios from 'axios' // v0.15.3
import httpAdapter from 'axios/lib/adapters/http'
import nock from 'nock' // v9.0.2

const host = 'http://localhost';

axios.defaults.host = host;
axios.defaults.adapter = httpAdapter;

describe('suite', () => {
  it('test', done => {
    nock(host)
      .get('/test')
      .reply(200, 'test data');

    axios.get('/test').then(response => {
      expect(response.data).to.be.equal('test data');
      done();
    });
  });
});
@shai32

This comment has been minimized.

shai32 commented Apr 6, 2017

to test the React lifecycle, I need to add
axios.defaults.adapter = httpAdapter;

not in the test file but in other files (my action creator that import axios, so adding the line in test will not help)
meaning this line will run also when I am not testing. does it affect how axios work and will cause problem in production?

@dschinkel

This comment has been minimized.

dschinkel commented Aug 10, 2017

nice. The bad thing about this is that now your tests need to know about axios not just nock. But glad you found a hybrid solution that allows you to use nock still.

I don't like the fact that my tests must know about axios. When I've used superagent, my tests used nock and didn't know jack about superagent which means my tests are more decoupled to the real implementation they are testing

@hems

This comment has been minimized.

hems commented Jul 4, 2018

I believe the ideal solution would be nock to transparently fallback to xhr-mock when appropriate?

as i suggested recently here: nock/nock#1160

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment