Created
May 4, 2017 14:55
-
-
Save achakravarty/0d8ae6e88a4d09b94e814f693e974a59 to your computer and use it in GitHub Desktop.
Testing Date with sinon using fakeTimers and spies
This file contains 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
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