admin管理员组文章数量:1426072
So I trying to Mock a date in my test, this is what I did :
const mockDate = new Date('2018-01-01');
const backupDate = Date;
beforeEach(() => {
(global.Date as any) = jest.fn(() => mockDate);
})
afterEach(() => {
(global.Date as any) = backupDate;
jest.clearAllMocks();
});
const backupDate = Date;
(global.Date as any) = jest.fn(() => mockDate);
expect(myModule).toMatchSnapshot();
(global.Date as any) = jest.fn(() => backupDate);
So this test in my local works fine and it's match with my snapshots :
exports[`should match with date`] = `
[MockFunction] {
"calls": Array [
Array [
Object {
"myDate" : "Mon Jan 01 2018 01:00:00 GMT+0100 (Central European Standard Time)"
}]]}
but in production environment I getting this instead which cause failing the test : Mon Jan 01 2018 01:00:00 GMT+0100 (CET)
Any idea what is wrong?
So I trying to Mock a date in my test, this is what I did :
const mockDate = new Date('2018-01-01');
const backupDate = Date;
beforeEach(() => {
(global.Date as any) = jest.fn(() => mockDate);
})
afterEach(() => {
(global.Date as any) = backupDate;
jest.clearAllMocks();
});
const backupDate = Date;
(global.Date as any) = jest.fn(() => mockDate);
expect(myModule).toMatchSnapshot();
(global.Date as any) = jest.fn(() => backupDate);
So this test in my local works fine and it's match with my snapshots :
exports[`should match with date`] = `
[MockFunction] {
"calls": Array [
Array [
Object {
"myDate" : "Mon Jan 01 2018 01:00:00 GMT+0100 (Central European Standard Time)"
}]]}
but in production environment I getting this instead which cause failing the test : Mon Jan 01 2018 01:00:00 GMT+0100 (CET)
Any idea what is wrong?
Share Improve this question edited Nov 29, 2018 at 22:40 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Nov 29, 2018 at 17:06 Emad DehnaviEmad Dehnavi 3,4515 gold badges23 silver badges47 bronze badges1 Answer
Reset to default 7You should use jest.spyOn works for locking time:
let dateNowSpy;
beforeAll(() => {
// Lock Time
dateNowSpy = jest.spyOn(Date, 'now').mockImplementation(() => 1487076708000);
});
afterAll(() => {
// Unlock Time
dateNowSpy.mockRestore();
});
For Date & time test on Jest, I wrote a module named jest-date-mock which will make Date & Time test simply and controllable.
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
test('usage', () => {
advanceTo(new Date(2018, 5, 27, 0, 0, 0)); // reset to date time.
const now = Date.now();
advanceBy(3000); // advance time 3 seconds
expect(+new Date() - now).toBe(3000);
advanceBy(-1000); // advance time -1 second
expect(+new Date() - now).toBe(2000);
clear();
Date.now(); // will got current timestamp
});
本文标签: javascriptMock a date in jest not working in different environmentStack Overflow
版权声明:本文标题:javascript - Mock a date in jest not working in different environment - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745447336a2658723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论