Jasmine testing nested angular promises not returning promise

783 views
Skip to first unread message

jm14...@gmail.com

unread,
Dec 4, 2015, 10:17:08 AM12/4/15
to Jasmine

I am trying to write get the test working that has nested promises. For some reason the promise is never returning.

For example


describe('Some tests', function ()
{
    it("Should complete the test with the nested promises", function(done)
    {
        inject(function (SomeService, $rootScope)
        {
            SomeService.somePromise().then(function(err)
            {
                 console.log("Message 1");
                 expect(err).toBeNull();
                 SomeService.someOtherPromise.then(function(res)
                 {
                     console.log("Message 2");
                     // Check some data
                     done();
                 });
            });
            $rootScope.$digest();
        });
    });
});


The strange thing is, if this test is run by its self it passes. But if there is more than one of these tests it fails.

When the test fails it seems as though the first promise is never returned. So we don't even see the first console message. My question is how to get this kind of test running and why is it not working correctly.

jm14...@gmail.com

unread,
Dec 4, 2015, 10:17:08 AM12/4/15
to Jasmine
This is on stack overflow here.

Gregg Van Hove

unread,
Dec 9, 2015, 6:32:12 PM12/9/15
to jasmi...@googlegroups.com
Do you have some other tests that use the mock clock (jasmine's or angular's) but don't clean it up? Many (most?, all?) promise libraries invoke their callbacks asynchronously (with `setTimeout` or similar), which would cause it to be kept in limbo and not actually invoke your `then`.

Hope this helps. Thanks for using jasmine!

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at http://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

jm14...@gmail.com

unread,
Dec 14, 2015, 4:01:07 AM12/14/15
to Jasmine
Yes, there would be other tests using this pattern. How would you clean them up?

Gregg Van Hove

unread,
Feb 16, 2016, 2:11:22 PM2/16/16
to jasmi...@googlegroups.com
Jasmine's mock clock should be uninstalled after each spec (`jasmine.clock().uninstall()`), so specs that don't intend to have it installed don't have to worry about this type of pollution.

I'm not fully clear on the mechanics of Angular's mock clock, so I'm not sure the best way to deal with it if that is what you're using.

-Gregg

Reply all
Reply to author
Forward
0 new messages