Created
July 6, 2017 20:37
-
-
Save todgru/447b34c95e07c164e5e3ad8f6056a05f to your computer and use it in GitHub Desktop.
Ava, Sinon, Moment testing time date
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
// I was testing some functionality that relied on `moment()`. | |
// I wanted to fake the value of the time Moment uses so I could test return values. | |
// Here is the method I used. Reference: https://stackoverflow.com/a/31591297/1478950 | |
const test = require("ava"); | |
const sinon = require("sinon"); | |
const moment = require("moment"); | |
let sandbox; | |
test.before(t => { | |
sandbox = sinon.sandbox.create(); | |
}); | |
test.afterEach.always(t => { | |
sandbox.restore(); | |
}); | |
test("should return mocked time", t => { | |
let testTime = "2017-07-04T00:00:00.000Z"; | |
sinon.useFakeTimers(new Date(testTime).getTime()); | |
t.is(moment().toISOString(), testTime); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment