[英]Jest useFakeTimers() blocks when used with firebase emulators
I'm trying to use jest.useFakeTimers()
to make my firebase
+ jest
tests stable when working with dates and timeouts.在处理日期和超时时,我正在尝试使用
jest.useFakeTimers()
使我的firebase
+ jest
测试稳定。 However, if I try to read anything from the emulated database the test times out and throws Exceeded timeout of 5000 ms for a test
.但是,如果我尝试从模拟数据库中读取任何内容,测试超时并抛出
Exceeded timeout of 5000 ms for a test
。 Using jest.setTimeout(30000)
doesn't help.使用
jest.setTimeout(30000)
没有帮助。
Github repro: https://github.com/vojdan/jest-fake-timers-firebase Github 重现: https://github.com/vojdan/jest-fake-timers-firebase
Here's the test:这是测试:
import * as admin from "firebase-admin";
// jest.setTimeout(30000);
describe("emulated firebase test", () => {
beforeAll(() => {
jest.useFakeTimers();
jest.setSystemTime(1349852318000);
admin.initializeApp({
projectId: "test-project",
databaseURL: "http://localhost:9000?ns=test-project",
});
});
afterAll(() => {
jest.useRealTimers();
});
it("only passes with fake timers", () => {
expect.assertions(1);
expect(new Date().valueOf()).toBe(1349852318000);
});
it("hangs when fake timers are used", async () => {
expect.assertions(1);
try {
const firestore = admin.firestore().doc("anything/really");
const realtime = admin.database().ref("anyhting/really");
console.log("this runs:", new Date().valueOf());
// ----- EVERYTHING RUNS UP TO HERE -----
// ----- any of the following commands will block when used with jest.useFakeTimers(); -----
// await firestore.get();
const ref = await realtime.once("value");
const value = ref.val();
console.log("this doesn't run:", value);
expect(1).toBe(1);
} catch (error) {
console.log("THERE WAS AN ERROR:", error);
}
});
});
Since useFakeTimers
isn't working for me, I'm using this as a workaround:由于
useFakeTimers
对我不起作用,我将其用作解决方法:
jest.mock('date-fns', () => {
// Require the original module to not be mocked...
const originalModule = jest.requireActual('date-fns');
return {
...originalModule,
getTime: jest.fn(() => mockedTimestamp),
getUnixTime: jest.fn(() => mockedTimestampUnix),
format: jest.fn(input => 'mockedTimeString'),
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.