您如何使用Jest模拟Firebase Firestore方法? [英] How do you mock Firebase Firestore methods using Jest?

查看:106
本文介绍了您如何使用Jest模拟Firebase Firestore方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列功能,每个功能执行各种Firestore交互.如何使用Jest模拟这些Firestore调用?我想避免使用图书馆.

I have a series of functions, each performing various firestore interactions. How do I use Jest to mock these firestore calls? I would like to avoid using a library.

当我使用jest.mock("firebase/app")jest.mock("firebase/firestore")及其它变体时,我得到的是null TypeErrors或表示我仍在引用实际导入而不是模拟的错误:Error: ... make sure you call initializeApp().

When I use jest.mock("firebase/app") and jest.mock("firebase/firestore") and other variations, I either get null TypeErrors, or errors indicating I am still referencing the actual import and not the mock: Error: ... make sure you call initializeApp().

例如,我要测试的一个简单功能:

For example, a simple function I want to test:

import firebase from "firebase/app";
import "firebase/firestore";

export const setDocData = (id, data) => {
  const newDoc = {
    created: firebase.firestore.FieldValue.serverTimestamp(),
    ...data
  };
  firebase
    .firestore()
    .doc("docs/" + id)
    .set(newDoc);
};

请注意如何照常导入firebase,然后导入firestore的副作用.还要注意,如何首先将Firestore调用为函数,然后再将其称为属性.我相信这是我麻烦的根源.

Notice how firebase is imported as usual, then firestore is imported a side effect. Also notice how firestore is called first as a function, then later referenced as a property. I believe this is the source of my trouble.

推荐答案

这是我找到的解决方案.网上没有太多有关此的信息,所以我希望它能对某人有所帮助.

Here is the solution I have found. There isn't much information online about this, so I hope it helps someone.

我相信您可以使用笑话来做类似的事情/__MOCKS__/ 文件夹系统,而不是像我在本文中那样覆盖firestore对象 例子.

I believe you can do something similar using jests /__MOCKS__/ folders system, rather than overwriting the firestore object as I do in this example.

诀窍是创建一个模拟函数的链接API,并将其设置在firebase对象上,而不是导入和模拟firestore.下面的示例使我可以测试上述示例函数以及doc().get() promises.

The trick is to create a chained API of mock functions, and set this on the firebase object, instead of importing and mocking firestore. The example below allows me to test the above example function, and also doc().get() promises.

const docData = { data: "MOCK_DATA" };
const docResult = {
  // simulate firestore get doc.data() function
  data: () => docData
};
const get = jest.fn(() => Promise.resolve(docResult));
const set = jest.fn();
const doc = jest.fn(() => {
  return {
    set,
    get
  };
});
const firestore = () => {
  return { doc };
};
firestore.FieldValue = {
  serverTimestamp: () => {
    return "MOCK_TIME";
  }
};

export { firestore };

我将其声明为在所有测试之前运行的文件中(参见文档),然后将其导入并在我的测试文件中使用,如下所示:

I declare it in a file that runs before all my tests do (see docs), and import and use it in my test files like this:

import firebase from "firebase/app";
import { firestore } from "../setupTests";
firebase.firestore = firestore;

describe("setDocData", () => {
  const mockData = { fake: "data" };
  beforeEach(() => {
    jest.clearAllMocks();
    setDocData("fakeDocID", mockData);
  });

  it("writes the correct doc", () => {
    expect(firestore().doc).toHaveBeenCalledWith("docs/fakeDocID");
  });

  it("adds a timestamp, and writes it to the doc", () => {
    expect(firestore().doc().set).toHaveBeenCalledWith({
      created: "MOCK_TIME",
      fake: "data"
    });
  });
});

这篇关于您如何使用Jest模拟Firebase Firestore方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆