简体   繁体   English

Jest useFakeTimers() 在与 firebase 模拟器一起使用时会阻塞

[英]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.

相关问题 尝试运行 firebase 仿真器时出现 Java 错误 - Java error when trying to run firebase emulators Firebase 模拟器可以用于 React 前端的集成测试吗? - Can Firebase Emulators be used for integration testing with a React frontend? 当函数目录位于不同的项目/位置时运行 firebase 模拟器 - Running the firebase emulators when the functions directory is in a different project/location 运行“firebase emulators:start”时如何隐藏 java 控制台? - How to hide java console when running "firebase emulators:start"? 带有模拟器的 Firebase 函数和存储 getSignedUrl - Firebase Functions and Storage getSignedUrl with Emulators 在 github 操作上运行 firebase 模拟器 - Run firebase emulators on github actions 是否可以将身份验证数据导入 Firebase 仿真器? - Is it possible to import Authentication data to Firebase Emulators? 与 Firebase 函数一起使用时,Nodemailer 无法正常工作 - Nodemailer not working when used with Firebase Functions 如何关闭本地 firebase 模拟器? - How can I shut down the local firebase emulators? 如何将新的 firebase 模拟器添加到我现有的项目中? - how to add new firebase emulators to my existing project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM