使用 mocha 测试时导入 firebase-functions-test 时出错

Error when import firebase-functions-test when testing with mocha

我正在尝试为 运行 mocha 测试设置一个 Firebase Cloud Functions 存储库。但是,当我使用 import * as firebase from "firebase-functions-test";const firebase = require("firebase-functions-test")(); 时,它会抛出以下错误。您可以在我的代码中看到我什至还没有调用实际的 firebase 函数,所以我认为这是一个设置问题。

问题:我需要对使用导入语法的 Firebase 函数测试进行 mocha 测试 运行ning 进行哪些更改?

工作测试代码

import { assert } from "chai";
describe("Sanity Check", () => {
  it("should pass", () => {
    assert.equal(0, 0);
  });
});

使用 require

的测试代码失败
const test = require("firebase-functions-test")();
import { assert } from "chai";
describe("Sanity Check", () => {
  it("should pass", () => {
    assert.equal(0, 0);
    test.cleanup();
  });
});

使用 import

的代码失败
import * as firebase from "firebase-functions-test";
import { assert } from "chai";

const test = firebase();
describe("Sanity Check", () => {
  it("should pass", () => {
    assert.equal(0, 0);
    test.cleanup();
  });
});

失败错误

> functions@ test /Users/cupidchan/temp/functions
> mocha -r ts-node/register test/**/*.spec.ts


Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/encoder' is not defined by "exports" in /Users/cupidchan/temp/functions/node_modules/firebase-functions/package.json
    at new NodeError (internal/errors.js:322:7)
    at throwExportsNotFound (internal/modules/esm/resolve.js:322:9)
    at packageExportsResolve (internal/modules/esm/resolve.js:545:3)
    at resolveExports (internal/modules/cjs/loader.js:450:36)
    at Function.Module._findPath (internal/modules/cjs/loader.js:490:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:888:27)
    at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/Users/cupidchan/temp/functions/node_modules/@cspotcode/source-map-support/source-map-support.js:679:30)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at Object.<anonymous> (/Users/cupidchan/temp/functions/node_modules/firebase-functions-test/lib/providers/firestore.js:26:19)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at Object.<anonymous> (/Users/cupidchan/temp/functions/node_modules/firebase-functions-test/lib/features.js:9:19)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at module.exports (/Users/cupidchan/temp/functions/node_modules/firebase-functions-test/lib/index.js:30:20)
    at Object.<anonymous> (/Users/cupidchan/temp/functions/test/index.spec.ts:9:14)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Module.m._compile (/Users/cupidchan/temp/functions/node_modules/ts-node/src/index.ts:1371:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/cupidchan/temp/functions/node_modules/ts-node/src/index.ts:1374:12)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at Object.exports.requireOrImport (/Users/cupidchan/temp/functions/node_modules/mocha/lib/nodejs/esm-utils.js:56:20)
    at async Object.exports.loadFilesAsync (/Users/cupidchan/temp/functions/node_modules/mocha/lib/nodejs/esm-utils.js:88:20)
    at async singleRun (/Users/cupidchan/temp/functions/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/Users/cupidchan/temp/functions/node_modules/mocha/lib/cli/run.js:374:5)
npm ERR! Test failed.  See above for more details.

package.json

{
  "name": "functions",
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log",
    "test": "mocha -r ts-node/register test/**/*.spec.ts --reporter spec"
  },
  "engines": {
    "node": "14"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1"
  },
  "devDependencies": {
    "@types/chai": "^4.2.22",
    "@types/mocha": "^9.0.0",
    "@types/node": "^16.11.11",
    "@typescript-eslint/eslint-plugin": "^3.9.1",
    "@typescript-eslint/parser": "^3.8.0",
    "chai": "^4.3.4",
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-import": "^2.22.0",
    "eslint-plugin-prettier": "^4.0.0",
    "esm": "^3.2.25",
    "firebase-functions-test": "^0.2.3",
    "mocha": "^9.1.3",
    "prettier": "^2.5.0",
    "ts-node": "^10.4.0",
    "typescript": "^3.8.0"
  },
  "private": true
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": ["src", "test"]
}

在指定

的最新版本后应该可以解决此错误
  • firebase-functionsv3.16.0

  • firebase-functions-test, v0.3.3.