-
Notifications
You must be signed in to change notification settings - Fork 27.5k
testing $q with mocha + chai + chai as promised does not work #9954
Comments
Would be so much easier to help if you would put this into a live plunker... Anyway, I think that the problem is that you are not calling At any rate this is a support question so would be better asked on StackOverflow. I would be happy to help - provided that you can put together a live reproduce scenario using plunker. |
@pkozlowski-opensource I have actually asked on stackoverflow and I haven't got an answer yet. And I have tried anything that you mentioned above, but that does not make it work. And I've tried to set up the test on jsfiddle but I cannot seem to make it work. I haven't tried setting up tests in the browser before so I have no clue what is going wrong there, but I do see some errors in the console. |
@rolandjitsu you didn't link properly back to SO question. As for the reproduce scenario I think jsfiddle is a pain since you've got limited control over initialisation part - this is why I've suggested http://plnkr.co/ |
I'm reading chai-as-promised, and I'm not seeing how you could possibly expect it to work with $q --- I've asked Domenic if anyones even tried this, but my guess is that it just won't work. Although it looks like it does support jQuery promises, interestingly. The thing is, it would need to basically monkey patch all of the methods in the promise API, in order to return a custom object, and that is kinda hard to do with $q. I might suggest filing a bug on his repo to get that working. |
@pkozlowski-opensource my bad, I've made the correction :) I've also made a plnkr fiddle, but I still get the same errors and I am not sure why ... |
@caitp It should be working since |
As it turns out, @caitp you're right. Because the "use strict";
describe.only("Promise", function () {
var $rootScope,
$scope,
$q;
beforeEach(angular.mock.inject(function (_$rootScope_, _$q_) {
$rootScope = _$rootScope_;
$q = _$q_;
$scope = $rootScope.$new();
}));
it("should resolve promise and eventually return", function (done) {
var defer = $q.defer();
defer.resolve("incredible, this doesn't work at all");
defer
.promise
.then(function (value) {
value.should.eql("incredible, this doesn't work at all");
done();
});
$scope.$apply();
});
it("should resolve promises as expected", function (done) {
var fst = $q.defer(),
snd = $q.defer();
fst
.promise
.then(function (value) {
value.should.eql("phew, this works");
});
snd
.promise
.then(function (value) {
value.should.eql("wow, this works as well");
});
fst.resolve("phew, this works");
snd.resolve("wow, this works as well");
$q
.all([
fst.promise,
snd.promise
])
.then(function () {
done();
});
$scope.$apply();
});
it("should reject promise and eventually return", function (done) {
var promise = $q.reject("no way, this doesn't work either?");
promise
["catch"](function (reason) {
reason.should.eql("no way, this doesn't work either?");
done();
});
$scope.$apply();
});
it("should reject promises as expected", function (done) {
var promise = $q.reject("sadly I failed for some stupid reason");
promise
["catch"](function (reason) {
reason.should.eql("sadly I failed for some stupid reason");
});
$q
.all([
promise
])
["catch"](function () {
done();
});
$scope.$apply();
});
}); I'm not sure, but maybe it would be a good idea to not have |
Hi it("should resolve promise and eventually return", function (done) {
var defer = $q.defer();
var promise = defer.promise;
setTimeout(function(){
defer.resolve("incredible, this doesn't work at all");
}, 1000);
promise.then(function (value) {
value.should.eql("incredible, this doesn't work at all");
done();
});
$scope.$apply();
}); it isn't working for me at all but is confusing whether or not this is either an angular fault or mocha fault. |
@rolandjitsu this is working as expected. |
Can we mention it in the docs? Consumed hours from my life |
I've modified the plinkr to work - http://plnkr.co/edit/WAvvu99uLhVRmdlwRWDv?p=preview So that is one way you can use them together.
See #3174 for why you need to set the exceptionHandlerProvider. |
@lukeapage thanks :) |
@lukeapage Thanks, that did the trick. |
Not that this adds any value but I find it funny that part of developer struggles this days is to make tests work!! May be it is time we re-examine how we are doing things. |
Hi @pkozlowski-opensource! I ran into the same problem and could make it work after I created a new scope for the test and did |
I have been trying for days to get
$q
to work with Mocha + Chai + Chai As Promised in Unit Tests, but for some reason, theafterEach
hook gets called after the Mocha timeout expires, thus my test fails.The following is a simple example I have been trying out:
The first, third and last tests are failing because of
Error: timeout of 2000ms exceeded
. I really cannot figure out what is the issue, but I've also opened up two more issues on both Mocha and Chai As Promised.It might be just me not calling the digest in the right place or might be the
$q
implementation or the other I mentioned that are not accounting for something inside the$q
implementation.The text was updated successfully, but these errors were encountered: