Skip to content

Commit

Permalink
use fake timers for promises.wait testing
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Jan 14, 2018
1 parent 65a0c45 commit 984ce4f
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/lib/promises.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert, expect } from "chai";
import { SinonFakeTimers, spy, useFakeTimers } from "sinon";
import { promisify, promisifyNoError, wait } from "./promises";
// tslint:disable:no-unused-expression

Expand Down Expand Up @@ -51,16 +52,27 @@ describe("lib/promises => promisifyNoError()", () => {
});

describe("lib/promises => wait()", () => {
let start;

let clock: SinonFakeTimers;
beforeEach(() => {
clock = useFakeTimers();
});

const timeout = 100;
const delta = 20;

it(`wait(${timeout}) should wait (${timeout}±${delta}) ms`, async function() {
// this is not super accurate, so retry a few times until it works
this.retries(10);
it(`wait(${timeout}) should wait ${timeout} ms`, (done) => {

const leSpy = spy();

wait(timeout).then(() => {
expect(Date.now()).to.equal(timeout);
done();
});
clock.runAll();

});

start = Date.now();
await wait(timeout);
expect(Date.now() - start).to.be.approximately(timeout, delta);
afterEach(() => {
clock.restore();
});
});

0 comments on commit 984ce4f

Please sign in to comment.