I use mocha-webpack for unit test an external api and I use superAgent for my ajax request. I make 2 test:
const request = require("superagent");
var chai = require('chai')
, expect = chai.expect
, should = chai.should();
var chaiAsPromised = require("chai-as-promised");
var chaiThings = require("chai-things");
chai.use(chaiThings);
chai.use(chaiAsPromised);
describe("Service API getData url", function () {
var response;
before(function(done) {
request
.get('http://www.google.fr/')
.withCredentials()
.end(function(res){
if ( err ) {
console.log("err", err)
} else {
response = res;
done();
}
});
});
it("response is 200", function() {
expect(response.status).to.equal(200);
});
it("response type", function() {
expect(response.type).to.equal("text/html");
});
but it doesn't work.
error : err { Error: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
On the Front, i don't have the prob, i can communicate with superAgent et display data, but not with the test.
my webpack config, I have :
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true"
},
inline: false,
noCredentials: true,
New test :
var response;
before(function(done) { request.get('https://httpbin.org/get').end(function(err, res){
if ( err ) {
console.log("err", err)
} else {
response = res;
done();
}
});
}); it("response is 200", function() {
expect(response.status).to.equal(200);
});
it("response type", function() {
expect(response.type).to.equal("application/json");
});
Test successfull
new test :
var response;
before(function(done) {
request.get('http://api.football-data.org/v1/fixtures?timeFrame=n1').set('X-Auth-Token', 'myid')
.end(function (err, res) {
if ( err ) {
console.log("err", err)
} else {
response = res;
done();
}
});
it("response is 200", function() {
expect(response.status).to.equal(200);
});
test error :
{ Error: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
crossDomain: true,
status: undefined,
method: 'GET',
url: 'http://api.football-data.org/v1/fixtures?timeFrame=n1' }
Everybody had the same prob ?