Last active
March 14, 2019 10:10
-
-
Save kephin/a270cf0d9a667119a852efcc57ffe162 to your computer and use it in GitHub Desktop.
Mocking current time for 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
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