halcwb
July 18, 2016, 1:31pm
1
When I run this test:
describe("Smoke process request tests", function () {
"use strict";
it("should echo the test request with response", function () {
var test = { act: 'test', qry: {} };
webix.ajax().post("http://localhost:3000/request", JSON.stringify(test))
.then(function (resp) {
expect(resp.json().requests.find(function (el) {
console.log(el);
return el.action === 'test';
})).to.eql(test);
done();
}).fail(function (errs) {
throw(errs);
});
});
});
the then or fail functions are never called. When I perform webix.ajax().sync() then I get an error.
How can I test ajax actions using Karma?
halcwb
July 18, 2016, 5:19pm
2
Am a bit further:
this works in the sense that the fail function is called:
it("should echo the test request with response", function (done) {
var test = { act: 'test', qry: {} };
var promise = webix.ajax().post("http://localhost:3000/request", JSON.stringify(test));
promise.then(function (resp) {
console.log('resp', app.util.inspect(resp));
done();
}).fail(function (err) {
console.log('err', app.util.inspect(promise));
throw(promise.state);
});
Exactly, the same code run directly in my browser, does result in a success and returns exactly what is expected.
maksim
July 19, 2016, 10:06am
3
Not quite sure about how Karma handles ajax call, but it can be a cross-domain call issue. If a main document of Karma was loaded from another domain or another port, all ajax calls to different domains will fail.
halcwb
July 19, 2016, 12:25pm
4
Thanks, it was a cross domain issue, karma uses its own server that serves the client but the backend server in that case is in a different domain. Adding the CORS headers solved the issue. Thanks for the response.