I've been attempting to configure a Firebase Cloud Functions repository for running mocha tests. However, I keep encountering an error when utilizing
import * as firebase from "firebase-functions-test";
or const firebase = require("firebase-functions-test")();
. Even though I haven't executed the actual Firebase functions in my code yet, it seems like there might be a setup issue.
Inquiry: What modification should I make to enable mocha testing for Firebase Functions using import syntax?
Successful test code
import { assert } from "chai";
describe("Sanity Check", () => {
it("should pass", () => {
assert.equal(0, 0);
});
});
Failed Test Code utilizing require
const test = require("firebase-functions-test")();
import { assert } from "chai";
describe("Sanity Check", () => {
it("should pass", () => {
assert.equal(0, 0);
test.cleanup();
});
});
Failed code utilizing 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();
});
});
Error message for the failure
> 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)
...
...
package.json
{
"name": "functions",
"scripts": {
...
...
},
"engines": {
"node": "14"
},
"main": "lib/index.js",
"dependencies": {
...
...
},
"devDependencies": {
...
...
},
"private": true
}
tsconfig.json
{
"compilerOptions": {
...
...
},
"compileOnSave": true,
"include": ["src", "test"]
}