Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jasmine.clock and setTimeout inside nested promises #1282

Closed
camel-chased opened this issue Feb 24, 2017 · 3 comments
Closed

jasmine.clock and setTimeout inside nested promises #1282

camel-chased opened this issue Feb 24, 2017 · 3 comments

Comments

@camel-chased
Copy link

camel-chased commented Feb 24, 2017

How to use jasmine.clock setTimeout mock inside nested promises? (result: Error: jasmine.DEFAULT_TIMEOUT_INTERVAL)

Order is crucial here.

I need to test nested promises that must have setTimeout inside - order matters. I know that then is kind of process.nextTick (or setImmediate) and it goes beyond current event loop (which is the core problem here) but this knowledge does not solve the problem :)

I know that I can put jasmine.clock().tick() inside nested promise but this is pointless because I'm testing proper order in some events related lib.

How to test something like this in jasmine? any ideas?

It looks like jamine.clock().tick(...) have a sync nature but promises are async. So we are hitting a wall here?

it("should run setTimeout mock inside chained promises",(done)=>{

      jasmine.clock().install();

      let realOrder = [];

      let ok1=new Promise((resolve,reject)=>{
          resolve("ok");
      }).then((ok)=>{
        let p=new Promise((resolve,reject)=>{
          setTimeout(()=>{ // not fired up due to 'then' method
            realOrder.push("1");
            resolve("1");
          },100);
        });
        //jasmine.clock().tick(101); //<- order is crucial here so I can't do that
        return p;
      });

      let ok2=new Promise((resolve,reject)=>{
          resolve("ok");
      }).then((ok)=>{
        let p=new Promise((resolve,reject)=>{
          setTimeout(()=>{ // not fired up due to 'then' method
            realOrder.push("2");
            resolve("2");
          },50);
        });
        //jasmine.clock().tick(51); //<- order is crucial here so I can't do that
        return p;
      });

      jasmine.clock().tick(151);// we must go outside nested promise - we dont need to run tick inplace because order will change
      Promise.all([ok1,ok2]).then((results)=>{
        expect(results).toEqual(["1","2"]);
        expect(realOrder).toEqual(["2","1"]);
        done();
      });
  });
@camel-chased camel-chased changed the title jamine.clock and setTimeout inside nested promises jasmine.clock and setTimeout inside nested promises Feb 24, 2017
@slackersoft
Copy link
Member

You are more likely to get quicker responses from the community for "How to use jasmine?" questions and a history of other solutions on the jasmine-js group. We try to keep jasmine's github issues list focused on bugs and feature requests for jasmine itself.

Closing. Thanks for using Jasmine!

@camel-chased
Copy link
Author

camel-chased commented Feb 24, 2017

This actually is a bug / feature.
setTimeout and jasmine.clock().tick() doesn't work inside nested promises.

For those who came along with same problem here is the custom solution:
http://stackoverflow.com/questions/42446795/how-to-use-jasmine-clock-settimeout-in-nested-promises-when-order-matters-ja

@slackersoft
Copy link
Member

The intention behind the Jasmine clock is to make actions that are normally async (possibly with a long wait) to be run in a synchronous manner. The way it does this is by mocking out the normally async methods (setTimeout and setInterval) so that callbacks passed to them can be triggered synchronously when your test wants them to. This means that if you do something async within an async callback, you may need to tick again to get that secondary async thing to happen. Within a given timeout length, the clock will call the functions in the order that they are added, so order should be preserved.

I hope this helps. Thanks for using Jasmine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants