You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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
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.
Reacted by Mart, Ali Ghanavatian, Hannan Ali, Kevin Carmody, Svyatoslav, Vasiliy Yorkin and Jake Regan
Reacted by Torsten Rüter, Richard Roncancio, Leonardo Ascione, Ali Ghanavatian, Hannan Ali and Jake Regan
Reacted by Richard Roncancio, Lucas Aragno, Ali Ghanavatian, Tsvetan Nenov, Hugh Boylan and Jake Regan
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 ;)
Reacted by Anton Zelenskii
Reacted by Rama Jakaria, Victor Longon, Titus Woo, Hannan Ali, Andrew Koroluk, Hugh Boylan, Paul O'Fallon, Erick B, Zonde, Loïc PIREZ and 6 more
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?
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
Reacted by Melissa Gattoni, Caroline Liu and Jimmy Breck-McKye
TL;DR - if you are using Jest and testing under node.js env add this to your jest.config.js testEnvironment: 'node'
Behind the scene
Some insight from my experience,
had the same issue, nock was failing when Axios initiated an OPTIONS call before every other method.
so using the adapter/http helped, but it's not the full picture.
cause in node, we do use the http adapter by default:
taken from axios.js
function getDefaultAdapter() {
var adapter;
if (typeof XMLHttpRequest !== 'undefined') {
// For browsers use XHR adapter
adapter = __webpack_require__(12);
} else if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
// For node use HTTP adapter
adapter = __webpack_require__(12);
}
return adapter;
}
so the issue here that the test environment DOES have XMLHttpRequest in the global scope because jest runs under jsdom by default. if your tests are running under node base environment you can add this to your jest.config testEnvironment: 'node'
Activity
chibicode commentedon 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:
My axios initialization:
Calling axios:
Then I get
Network Erroron the test.log(console.log)on nock outputs this: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 commentedon May 3, 2016
@chibicode yeah, this is probably what is happening, have you been able to fix this?
sathify commentedon May 6, 2016
Same problem here not able to test with nock.
mzabriskie commentedon May 31, 2016
I don't have much experience with nock, but I have a couple suggestions for what it's worth.
OPTIONSmethod and are running in node you can force the node adapter:axios.defaults.adatper = require('axios/lib/adapters/http');mzabriskie commentedon Jul 16, 2016
Closing due to inactivity. Hoping that
moxiosis solving the problem for everyone :)ConAntonakos commentedon Sep 27, 2016
Edit: Oh, my. I just realized that the
defaults.adapter = ...was misspelled. TheNetwork Errordisappeared at least!Using:
axios 0.14.0react 15.3.1nock 8.0.0I 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
nockto mock the API request, and testing anaxiosrequest 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:
jordanpapaleo commentedon 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 commentedon Jan 12, 2017
Works for me:
shai32 commentedon 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 commentedon 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 commentedon 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
WhoAteDaCake commentedon May 23, 2019
If you use axios v18.0, make sure that you set the nock host to be the same as axios baseURL
AlonMiz commentedon Mar 21, 2020
TL;DR - if you are using Jest and testing under node.js env add this to your
jest.config.jstestEnvironment: 'node'Behind the scene
Some insight from my experience,
had the same issue, nock was failing when Axios initiated an OPTIONS call before every other method.
so using the
adapter/httphelped, but it's not the full picture.cause in node, we do use the http adapter by default:
taken from axios.js
so the issue here that the test environment DOES have
XMLHttpRequestin the global scope because jest runs under jsdom by default. if your tests are running undernodebase environment you can add this to yourjest.configtestEnvironment: 'node'