14

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/' + username).log(console.log)
.get('/')
.query(true)
.reply(400, {
  statusMessage: 'Bad Request',
  foo: 'foo'
})

http.get('https://api.github.com/users/' + username, function(res) {
  console.log('res', res)
})

I also added this middleware

const middlewares = [thunk];
const mockStore = configureStore(middlewares);

====== UPDATE Jun 6 ======

Whole flow using react-redux Here is my test:

import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import axios from 'axios';
import expect from 'expect';
import * as actions from 'actions/test-actions'
import * as types from 'types';
import nock from 'nock'
import { username } from 'constansts'

const middlewares = [thunk];
const mockStore = configureStore(middlewares);

describe('Asynchronous actions', () => {
  it('Basic example', done => {
    nock('https://api.github.com')
    .get('/users/' + username)
    .reply(400, {
      statusMessage: 'Bad Request',
      foo: 'foo'
    })

    var expectedActions = []
    let store = mockStore([], expectedActions, done)

    store.dispatch(actions.testRequest())
      .then(() => {
        console.log('store.getActions() => ', store.getActions())
      })
      .then(done).catch((err) => {
        console.log('ERROR==>', err)
        done()
      })
  })
})

And here is the action

export function testRequest () {
  return axios.get('https://api.github.com/users/' + username)
  .then(function (res) {
    console.log('response =>', res.status)
  })
  .catch(function (err) {
    console.log('error =>', err)
  })
}

res.status is 200, even if I use nock for changing to 400

nico.amabile
  • 397
  • 2
  • 11
  • Take a look: https://github.com/node-nock/nock/issues/150. I too cannot get my client not hit nock server, although I am using axios. It appears that it does not work for many people – Alex Panov Jun 06 '16 at 15:23
  • @AlexPanov Thanks! I also tried with fetch-mock and isomorphic-fetch, the same here, I still getting real data back – nico.amabile Jun 06 '16 at 16:02

4 Answers4

1

This is an old question but I believe the answer is that you need to set the axios http adapter:

import axios from 'axios';
axios.defaults.adapter = require('axios/lib/adapters/http');

When running tests with jest you generally run them in a "browser like" environment. To get axios to use the node http library instead you need to specifically tell it to use the http adapter.

https://github.com/axios/axios/tree/master/lib/adapters

Clarkie
  • 6,818
  • 8
  • 33
  • 52
  • To clarify: In the browser axios uses XHR which nock cannot intercept. That fix forces axios to use the http adapter for nock usage. In NodeJS http is the default and the adapter patch is not needed. – cyberwombat Jul 09 '20 at 13:46
0

You should specify the path in the get method:

nock('https://api.github.com').log(console.log)
  .get('/users/' + username)
  .query(true)
  .reply(400, {
    statusMessage: 'Bad Request',
    foo: 'foo'
  });
MrWillihog
  • 2,266
  • 16
  • 17
  • I tried with this, didn't work either. Am I missing something on karma config? I also tried with the whole flow of react-redux, calling the action with mockStore. I'm pretty sure I'm missing some basic step, I can't figure out what it is – nico.amabile Jun 06 '16 at 15:30
0

I found the answer!

Aparently there is no compatibility with axios, I also tried with 'fetch', 'isomorphic-fetch' but no luck.

'whatwg-fetch' was the answer

Thanks very much and I hope this post helps someone else.

import 'whatwg-fetch'
export function testRequest () {
  return fetch('https://api.github.com/users/' + username)
  .then(function (res) {
    console.log('response =>', res.status)
  })
  .catch(function (err) {
    console.log('error =>', err)
  })
}
nico.amabile
  • 397
  • 2
  • 11
  • 1
    I don't think this information is accurate. We are using axios and nock together in our project successfully. You likely had some issue that you just didn't figure out. – Matt Huggins Jun 07 '16 at 00:57
  • I've found that the latest versions of axios and nock do work together, (tested nock v8.0.0) but older versions of nock didn't work with axios. (tested nock v0.59.1) Try updating and seeing if that helps. – dncook Jun 21 '16 at 06:02
  • FWIW I recently discovered that `axios@0.14.0` is incompatible with `nock@8.0.0` when I updated axios. Rolling back to `axios@0.13.1` fixed the problem. – gfullam Sep 28 '16 at 15:32
0

Are you running your tests in a node environment or in a web browser (like PhantomJS)?

In order to use nock you must run your tests in node (using Jest or mocha), nock overrides node http behavior and for that reason it only works in node and not in browsers (like PhantomJS).

To have your test running you can:

  • run it in a node environment (like Jest or mocha)
  • or use a different library to mock like fetch-mock
mario.ecc
  • 130
  • 1
  • 8