无法从“node_modules/firebase-functions-test/lib/providers/firestore.js”找到模块“firebase-functions/lib/encoder”

Luc*_*ner 2 firebase google-cloud-functions google-cloud-firestore

嘿,我正在尝试在这里对这个云功能进行单元测试:

import { logger, region, https } from "firebase-functions/v1";
import Message from "../types/message";

const helloWorldHandler = region("europe-west1").https.onCall((_, context) => {
  if (context.app == undefined) {
    throw new https.HttpsError("failed-precondition", "The function must be called from an App Check verified app.");
  }

  logger.info("Hello logs!", { structuredData: true });
  const message: Message = {
    text: "Hello from Firebase!",
    code: 200,
  };
  return message;
});

export default helloWorldHandler;
Run Code Online (Sandbox Code Playgroud)

通过以下测试:

import * as functions from "firebase-functions-test";
import * as path from "path";

const projectConfig = {
  projectId: "myproject-id",
};

const testEnv = functions(projectConfig, path.resolve("./flowus-app-dev-fb-admin-sdk-key"));
// has to be after initializing functions
import helloWorldHandler from "../src/functions/helloworld";
import Message from "../src/types/message";

describe('Testing "helloWorld"', () => {
  const helloWorld = testEnv.wrap(helloWorldHandler);

  it("helloWorld does work", async () => {
    const data = {};

    const success: Message = await helloWorld(data);
    expect(success.code).toBe(200);
  });
});
Run Code Online (Sandbox Code Playgroud)

当我运行它时,yarn test我收到以下错误 Cannot find module 'firebase-functions/lib/encoder' from 'node_modules/firebase-functions-test/lib/providers/firestore.js' 即使我的函数一开始根本不使用 firestore?

有任何想法吗 ?

小智 13

我在尝试为 Firebase 云功能设置单元测试环境时遇到了类似的问题。主要是,在遵循Firebase 文档上的所有步骤并运行后,npm test 我会收到以下错误

错误 [ERR_PACKAGE_PATH_NOT_EXPORTED] 包子路径“./lib/encoder”未由“exports”定义

在偶然发现Farid 对这个问题的建议后,我意识到,由于某种原因,npm i firebase-functions-test没有安装该模块的最新版本。

尝试npm i firebase-functions-test@latest