Environment
node: 14.15.5
OS: macOS Big Sur v11.2
What is the problem
When I try to run test with Jest, test fails with an error message that "TypeError: firebase__default.default.initializeApp is not a function"
even if I use initializeTestApp, not initializeApp
What I have tried
- I used
@firebase/rules-unit-testing ver2.0.0but there was noinitializeTestAppfunction so I downgraded the version to 1.3.14 as described below. - I deleted
yarn.lockfile and use commandyarn installbut it did not work. - I searched issues but there is no exact the same issue.(this is similar issue: https://github.com/firebase/firebase-js-sdk/issues/4944)
I would really appreciate if you can give me any advices. Thank you.
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"
}
}