使用 mocha 进行测试时导入 firebase-functions-test 时出错
Posted
技术标签:
【中文标题】使用 mocha 进行测试时导入 firebase-functions-test 时出错【英文标题】:Error when import firebase-functions-test when testing with mocha 【发布时间】:2022-01-07 23:44:45 【问题描述】:我正在尝试设置 Firebase Cloud Functions 存储库来运行 mocha 测试。但是,当我使用import * as firebase from "firebase-functions-test";
或const firebase = require("firebase-functions-test")();
时会引发以下错误。您可以在我的代码中看到我还没有调用实际的 firebase 函数,所以我认为这是一个设置问题。
问题:我需要进行哪些更改才能使 mocha 测试运行以使用导入语法进行 Firebase Functions 测试?
工作测试代码
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"]
【问题讨论】:
【参考方案1】:这个错误应该在指定最新版本后解决
firebase-functions
、v3.16.0 和
firebase-functions-test
, v0.3.3.
【讨论】:
以上是关于使用 mocha 进行测试时导入 firebase-functions-test 时出错的主要内容,如果未能解决你的问题,请参考以下文章
无法导入 Reactjs 类以使用 mocha 和酶进行测试
Mocha + TypeScript:不能在模块外使用导入语句