我试图模拟一个将用于所有单元测试用例的日期对象。我有很多关于javascript的参考,但是由于类型错误,在typescript中不起作用。谁能提供解决此问题的解决方案?
// sampleMethod() Test Suite
describe('sample Method', () => {
it('should match two objects', () => {
// Preparing
const sampleRequest: sampleRequestType = {
key: '0',
status: 'success',
creation_time: new Date(),
attempts: 0,
last_attempt_time: new Date()
};
// Executing
const result = sampleobject.sampleMethod();
// Verifying
expect(result).toEqual(sampleRequest); // assume result also return the same value as sampleRequest
});
});
最佳答案
由于Jest是在茉莉之上开发的,因此您可以使用它
const mockDate = new Date(1434319925275);
spyOn(window, 'Date').and.callFake(() => {
return mockDate;
});