Sinon: setTimeout wrapped in a Promise is not triggered while using fake timers

Created on 27 Mar 2017  路  1Comment  路  Source: sinonjs/sinon

Don't know if this is the best place to report this. If it is not, please point me to where should I report this.

  • Sinon version: 2.1.0
  • Environment: Node v6.9.x
  • Other libraries you are using: Promise (native), mocha

What did you expect to happen?

While using sinon fake timers, I expect that timeouts set inside a promise, which is wrapped in another promise, get triggered.

What actually happens

Timeouts set inside a Promise, which is wrapped in another promise, are not triggered.

How to reproduce

This works perfectly fine:

it(`should delay execution by 1 second`, function () {
  const clock = sandbox.useFakeTimers();

  const p = new Promise(function (resolve) {
    setTimeout(resolve, 1000);
  });

  clock.tick(1000);

  return p;
});

This doesn't work as expected, i.e. the resolve never gets called because the setTimeout callback is never triggered:

it(`should delay execution by 1 second`, function () {
  const clock = sandbox.useFakeTimers();

  const p = Promise.resolve()
    .then(() => {
      return new Promise(function (resolve) {
        setTimeout(resolve, 1000); // resolve never gets called
      });
    });

  clock.tick(1000);

  return p;
});

Most helpful comment

Solved it. For future reference, this was what led me to the solution:
http://stackoverflow.com/a/43048888/582838

>All comments

Solved it. For future reference, this was what led me to the solution:
http://stackoverflow.com/a/43048888/582838

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fearphage picture fearphage  路  3Comments

stevenmusumeche picture stevenmusumeche  路  3Comments

zimtsui picture zimtsui  路  3Comments

byohay picture byohay  路  3Comments

OscarF picture OscarF  路  4Comments