Skip to content

Instantly share code, notes, and snippets.

@kephin
Last active March 14, 2019 10:10
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 kephin/a270cf0d9a667119a852efcc57ffe162 to your computer and use it in GitHub Desktop.
Save kephin/a270cf0d9a667119a852efcc57ffe162 to your computer and use it in GitHub Desktop.
Mocking current time for Date
describe('...', () => {
const RealDate = Date;
function mockDate(isoDate) {
global.Date = class extends RealDate {
constructor(...args) {
if (args.length) return new RealDate(...args);
return new RealDate(isoDate);
}
};
}
afterEach(() => {
global.Date = RealDate;
});
it('...', () => {
mockDate('2018-01-01');
// rest of the code
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment