Skip to content

Instantly share code, notes, and snippets.

@dualcyclone
Forked from wrumsby/test-simple.js
Last active January 16, 2023 07:19
Show Gist options
  • Save dualcyclone/0480ccab01aa89383c37429d3be6cb64 to your computer and use it in GitHub Desktop.
Save dualcyclone/0480ccab01aa89383c37429d3be6cb64 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;
var mockDate;
before(function () {
var date = new Date(2013, 3, 1);
// Maintaining use of fake timers for Date
clock = sinon.useFakeTimers(date.getTime());
// Implementing method to be able to assert calls to Date
mockDate = sinon.spy(global, 'Date');
});
it('should append the date to the given value', function () {
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);
expect(mockDate.calledWithNew()).to.be.true;
});
after(function () {
clock.restore();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment