【发布时间】:2020-08-03 12:40:22
【问题描述】:
正如标题所说,在 Jest 中模拟对 new Date() 构造函数的调用时我遇到了一些困难。我通过返回一个特定的日期时间来模拟这个调用,以确保我的测试不会失败。但是,由于日期构造函数在我正在测试的函数中被调用了两次,所以第二次调用也对其实现进行了模拟,不应模拟第二次调用。
我在下面提供了一个上下文示例,它是一个返回明天之前的秒数的小函数,因此需要模拟一致的 now 时间。
任何帮助都会有很大帮助,我已经通过 Stack Overflow 和 Jest 文档进行了搜索。
提前致谢。
功能
function getSecondsToTomorrow() {
// This call needs to be mocked to always return the same date time
const now = new Date();
// This call should not be mocked
const tomorrow = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() + 1,
);
return Math.round( (tomorrow - now) / 1000);
}
测试
describe("getSecondsToTomorrow()", () => {
let result, mockDate, now;
beforeAll(() => {
now = new Date(2020, 7, 3, 23, 0, 0);
mockDate = jest.spyOn(global, "Date").mockImplementation(() => now);
result = getSecondsToTomorrow();
});
afterAll(() => {
mockDate.mockRestore();
});
it("should return the number of seconds until tomorrow, from the time the function was called", () => {
// We mock the date to be 2300, so the return should be 1 hour (3600 seconds)
expect(result).toBe(3600);
});
});
更新
虽然我还没有找到问题的具体答案,但我找到了解决方法。即使用 Jest 的 mockImplementationOnce 方法,我可以在第一次调用 new Date() 时模拟日期时间,并在第二次调用时将任何参数作为默认的 mockImplementation 传递。
describe("getSecondsToTomorrow()", () => {
let result, mockDate, now, dateClone;
function setUpDateMock(now) {
return jest
.spyOn(global, "Date")
.mockImplementation((...args) => new dateClone(...args))
.mockImplementationOnce(() => now)
}
beforeAll(() => {
dateClone = Date;
now = new Date(2020, 7, 3, 23, 0, 0);
mockDate = setUpDateMock(now);
result = getSecondsToTomorrow();
});
afterAll(() => {
mockDate.mockRestore();
});
it("should return the number of seconds until tomorrow, from the time the function was called", () => {
expect(result).toBe(3600);
});
});
【问题讨论】:
-
将被测代码改为使用
new Date(Date.now()),然后模拟Date.now()。 -
感谢您的回复。但是我需要监视对 Date 构造函数的调用,此实现仍将覆盖对
new Date()的第二次调用,尽管将模拟调用传递给Date.now()。我需要一种让 Jest 在第二次调用new Date()时忽略模拟实现的方法
标签: javascript unit-testing mocking jestjs