Skip to content

Instantly share code, notes, and snippets.

@achakravarty
Created May 4, 2017 14:55
Show Gist options
  • Save achakravarty/0d8ae6e88a4d09b94e814f693e974a59 to your computer and use it in GitHub Desktop.
Save achakravarty/0d8ae6e88a4d09b94e814f693e974a59 to your computer and use it in GitHub Desktop.
Testing Date with sinon using fakeTimers and spies
const sinon = require('sinon');
const { expect } = require('chai');
describe('Timer', () => {
it('should verify Date constructor called', () => {
let dateSpy = sinon.spy(global, 'Date');
const date = new Date();
expect(dateSpy.calledWithNew()).to.be.true;
dateSpy.restore();
});
it('should verify Date object using fakeTimer', () => {
const expectedDate = new Date();
expectedDate.setTime(expectedDate.getTime() + 1000)
const clock = sinon.useFakeTimers(expectedDate.getTime());
const actualTime = new Date();
expect(actualTime.getTime()).to.equal(clock.now);
clock.restore();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment