【发布时间】:2021-10-29 18:58:25
【问题描述】:
环境
节点:14.15.5
操作系统:macOS Big Sur v11.2
有什么问题
当我尝试使用 Jest 运行测试时,测试失败并显示错误消息“TypeError: firebase__default.default.initializeApp is not a function”
即使我使用initializeTestApp,而不是initializeApp
我尝试过的
- 我使用了
@firebase/rules-unit-testing ver2.0.0,但没有initializeTestApp功能,所以我将版本降级到1.3.14,如下所述。 - 我删除了
yarn.lock文件并使用命令yarn install但它不起作用。 - 我搜索了问题,但没有完全相同的问题。(这是类似的问题:https://github.com/firebase/firebase-js-sdk/issues/4944)
如果您能给我任何建议,我将不胜感激。谢谢。
firebaseRules.test.ts
import * as firebase from "@firebase/rules-unit-testing";
import fs from "fs";
import path from "path";
const myId = "user_Id";
// Load security rules
const filePath = path.join(__dirname, "./firestore.rules");
const rules = fs.readFileSync(filePath, "utf8");
const myProjectId = "my-project-2021";
describe("Allow access to users collection only when user is authorized", () => {
beforeEach(
async () => await firebase.clearFirestoreData({ projectId: myProjectId })
);
beforeEach(
async () =>
await firebase.loadFirestoreRules({
projectId: myProjectId,
rules: rules,
})
);
afterAll(
async () => await firebase.clearFirestoreData({ projectId: myProjectId })
);
describe("allow access to project collection only when user is authorized", () => {
test("fail to access without auth", async () => {
const db = firebase
// here test fails: TypeError: firebase__default.default.initializeApp is not a function
.initializeTestApp({
projectId: myProjectId,
})
.firestore();
const doc = db
.collection("users")
.doc("user_shogo")
.collection("projects")
.doc("testProjectId");
await firebase.assertFails(doc.get());
});
});
});
package.json
{
"name": "move-gantt",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"test": "jest --silent=false --verbose false"
},
"dependencies": {
"@firebase/rules-unit-testing": "1.3.14",
"@material-ui/core": "^4.12.3",
"@material-ui/icons": "^5.0.0-beta.4",
"@material-ui/lab": "^4.0.0-alpha.60",
"@material-ui/styles": "^4.11.4",
"@types/react-redux": "^7.1.18",
"date-fns": "^2.23.0",
"firebase": "8.10.0",
"firebase-tools": "^9.16.6",
"next": "11.0.1",
"next-transpile-modules": "^8.0.0",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-redux": "^7.2.4",
"redux": "^4.1.1",
"redux-devtools-extension": "^2.13.9",
"redux-logger": "^3.0.6",
"styled-components": "^5.3.0",
"styled-jsx": "3.4.5"
},
"devDependencies": {
"@babel/cli": "^7.14.8",
"@babel/core": "^7.15.0",
"@testing-library/react": "^12.0.0",
"@types/jest": "^27.0.0",
"@types/node": "^16.4.11",
"@types/react": "^17.0.15",
"@types/react-dom": "^17.0.9",
"@types/react-test-renderer": "^17.0.1",
"@types/redux-logger": "^3.0.9",
"@types/styled-components": "^5.1.11",
"babel-plugin-styled-components": "^1.13.2",
"eslint": "7.32.0",
"eslint-config-next": "11.0.1",
"firebase-admin": "9.8.0",
"jest": "^27.0.6",
"react-test-renderer": "^17.0.2",
"ts-jest": "^27.0.4",
"typescript": "^4.3.5"
}
}
【问题讨论】:
标签: typescript firebase unit-testing google-cloud-firestore