Skip to content

Instantly share code, notes, and snippets.

@Farenheith
Created February 21, 2020 08:58
Show Gist options
  • Save Farenheith/1e11c1df3480dacfcb58ef9bc3c7d258 to your computer and use it in GitHub Desktop.
Save Farenheith/1e11c1df3480dacfcb58ef9bc3c7d258 to your computer and use it in GitHub Desktop.
Another way to test global functions
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