Skip to content

Instantly share code, notes, and snippets.

@Farenheith
Last active February 21, 2020 09:01
Show Gist options
  • Save Farenheith/28c0c515808b3bacc95736ccfbb67afb to your computer and use it in GitHub Desktop.
Save Farenheith/28c0c515808b3bacc95736ccfbb67afb to your computer and use it in GitHub Desktop.
Unit test for global timing functions
import { expect } from 'chai';
import { SinonFakeTimers, useFakeTimers, stub, match } from 'sinon';
import { doubleConsole } from '../src/double-console';
describe.only('doubleConsole()', () => {
let clock: SinonFakeTimers;
beforeEach(() => {
clock = useFakeTimers();
stub(global, 'setImmediate').callThrough();
stub(global, 'setTimeout').callThrough();
stub(console, 'log');
});
it('should call console.log twice with a 100 ms interval', () => {
doubleConsole();
expect(console.log).calledOnceWithExactly('Now you see me');
expect(global.setTimeout).to.have.been.calledOnceWithExactly(match.func, 100);
expect(global.setImmediate).to.have.been.calledOnceWithExactly(match.func);
clock.tick(0);
expect(console.log).calledTwice.calledWithExactly('Now still');
clock.tick(100);
expect(console.log).calledThrice.calledWithExactly("Now you don't");
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment