Skip to content

Instantly share code, notes, and snippets.

@wrumsby
Last active May 4, 2017 10:06
Show Gist options
  • Save wrumsby/5335617 to your computer and use it in GitHub Desktop.
Save wrumsby/5335617 to your computer and use it in GitHub Desktop.
Example of how to use Sinon's useFakeTimers.
/*global define, describe, it, before, after */
define(['chai', 'sinon', 'boilerplate/simple'], function (chai, sinon, simple) {
'use strict';
var assert = chai.assert;
describe('simple', function () {
describe('stamp', function () {
var clock;
before(function () {
var date = new Date(2013, 3, 1);
clock = sinon.useFakeTimers(date.getTime());
});
it('should append the date to the given value', function () {
var actual = simple.stamp('test');
assert.equal(actual, 'test:2013-04-01');
});
after(function () {
clock.restore();
});
});
});
});
@dualcyclone
Copy link

To make this more resilient, your test should really be:

var actual = simple.stamp('test');
var expected = 'test:' + date.getFullYear() + '-' + ("0" + (date.getMonth() + 1)).slice(-2) + '-' + ("0" + date.getDate()).slice(-2);

assert.equal(actual, expected);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment