Last active
February 21, 2020 09:01
-
-
Save Farenheith/28c0c515808b3bacc95736ccfbb67afb to your computer and use it in GitHub Desktop.
Unit test for global timing functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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