nico.amabile
nico.amabile

Reputation: 495

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/' + 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

Upvotes: 17

Views: 18151

Answers (5)

William Myers
William Myers

Reputation: 11

I've recently published an alternative to nock called wirepig. Since it doesn't rely on overriding any node stdlib internals, it's compatible with any HTTP library your application uses, including all versions of axios.

The API is fairly similar; give it a try if you're still struggling with this!

Upvotes: 0

Clarkie
Clarkie

Reputation: 7550

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

Upvotes: 3

mario.ecc
mario.ecc

Reputation: 150

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

Upvotes: 1

nico.amabile
nico.amabile

Reputation: 495

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)
  })
}

Upvotes: 0

MrWillihog
MrWillihog

Reputation: 2646

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'
  });

Upvotes: 2

Related Questions