Skip to content

Instantly share code, notes, and snippets.

@todgru
Created July 6, 2017 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todgru/447b34c95e07c164e5e3ad8f6056a05f to your computer and use it in GitHub Desktop.
Save todgru/447b34c95e07c164e5e3ad8f6056a05f to your computer and use it in GitHub Desktop.
Ava, Sinon, Moment testing time date
// 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