This seems a bit weird to me. I\'m trying to test an actual (ie. real network) request with Jest.
These are the tested scenarios:
That is funny,that the axios used XMLHttpRequest by primary,and ajax request can't access across domain,so your test failed ,so you can let your code pass by set the axios adapter.
function getDefaultAdapter() {
var adapter;
if (typeof XMLHttpRequest !== 'undefined') {
// For browsers use XHR adapter
adapter = require('./adapters/xhr');
} else if (typeof process !== 'undefined') {
// For node use HTTP adapter
adapter = require('./adapters/http');
}
return adapter;
}
import axios from 'axios';
//This WORKS
test('testing with headers', (done) => {
var path=require('path');
var lib=path.join(path.dirname(require.resolve('axios')),'lib/adapters/http');
var http=require(lib);
axios.get('http://192.168.1.253', {
adapter: http,
headers: {
Authorization: "Basic YWRtaW46bHVveGlueGlhbjkx"
}
}).then((res) => {
expect(res.status).toBe(200);
done();
}).catch(done.fail);
});
"jest": {
"testURL":"http://192.168.1.253"
}
then the test can be access http via ajax
import axios from 'axios';
//This WORKS
test('testing with headers', (done) => {
axios.get('http://192.168.1.253', {
headers: {
Authorization: "Basic YWRtaW46bHVveGlueGlhbjkx"
}
}).then((res) => {
expect(res.status).toBe(200);
done();
}).catch(done.fail);
});