vendredi 24 février 2017

how to use jasmine clock (setTimeout) in nested promises when order matters? (jasmine.DEFAULT_TIMEOUT_INTERVAL error)

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("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();
      });
  });

Aucun commentaire:

Enregistrer un commentaire