Created
February 21, 2020 08:58
-
-
Save Farenheith/1e11c1df3480dacfcb58ef9bc3c7d258 to your computer and use it in GitHub Desktop.
Another way to test global 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 { match, stub } from 'sinon'; | |
import { doubleConsole } from '../src/double-console'; | |
describe.only('doubleConsole()', () => { | |
let callbackSetImmediate: any; | |
let callbackSetTimeout: any; | |
beforeEach(() => { | |
stub(global, 'setImmediate').callsFake((x) => { | |
callbackSetImmediate = x; | |
return undefined as any; | |
}); | |
stub(global, 'setTimeout').callsFake((x) => { | |
callbackSetTimeout = x; | |
return undefined as any; | |
}); | |
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); | |
callbackSetImmediate(); | |
expect(console.log).calledTwice.calledWithExactly('Now still'); | |
callbackSetTimeout(); | |
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