You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to test my cloud Firestore Security rules using the Firebase Emulators. Emulators work fine for manual tests, but when I try to run automated tests with Jest (React Scripts to be specific), I have the following error:
create a new project with npx create-react-app my-project
create a Firebase project, and enable Firestore
Install Firebase Emulators with ǹpm install -g firebase-tools`
write some test using Jest, example in file named myTest.test.js
run Firebase Emulators with firebase emulators:start
run tests with npm test
Relevant Code:
Test code:
describe('The Cloud Firestore Security Rules',()=>{lettestEnv,unauthDbbeforeAll(async()=>{testEnv=awaitinitializeTestEnvironment({projectId: 'mtg-deck-planner-11665',firestore: {rules: readFileSync(resolve(__dirname,'../../firestore.rules'),'utf-8'),host: 'localhost',port: '9000'}})unauthDb=testEnv.unauthenticatedContext().firestore()})afterAll(()=>{testEnv.cleanup()})afterEach(async()=>{awaittestEnv.clearFirestore()})it('should not allow any unauthenticated user to write to the database',async()=>{constdocRef=doc(unauthDb,'/any','docId')awaitassertFails(setDoc(docRef,{data: 'test'}))})it('should not allow any unauthenticated user to view users\' data',async()=>{constcollectionPath='users'constdocPath='userId'awaittestEnv.withSecurityRulesDisabled((amdinCtx)=>setDoc(doc(amdinCtx.firestore(),collectionPath,docPath),{uid: 'user_123'}))constdocRef=doc(unauthDb,collectionPath,docPath)awaitassertFails(getDoc(docRef))})})
First test pass and fail for the correct reason. Second test exit the Node process with the aforementioned error message.
Firestore rules
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
match /users/{userId} {
allow read, write: if false;
}
}
}
run Firebase Emulators with firebase emulators:start
run tests with npm test
This was two commands in separate terminals. But I since had a better outcome running firebase emulators:exec "node ./path/to/my/test-script.js": it worked with every Testing SDK feature (9.15.0).
Are the aforementioned steps 5 and 6 supposed to work just as well as the emulators:exec command?
Hello,
Solved this error by setting testEnvironment to node as suggested in the answer on SO : https://stackoverflow.com/a/63465382
You can do this either by creating a jest.config.js file or by passing the --env=node flag to the jest command.
As I understand it, jsdom implements a browser environment and is useful for UI tests. While testing firebase security rules, we want a node 'server' environment.
This may seem obvious to many, but perhaps it could be included in the firebase documentation ?
I get this error when I setup firestore listeners on multiple docs. It happens both when I'm using firestore emulator and also in production.
Mine is Error: FIRESTORE (9.18.0) INTERNAL ASSERTION FAILED: Unexpected state
9.18.0
Describe your environment
Describe the problem
I am trying to test my cloud Firestore Security rules using the Firebase Emulators. Emulators work fine for manual tests, but when I try to run automated tests with Jest (React Scripts to be specific), I have the following error:
Stack trace points here:
firebase-js-sdk/packages/firestore/src/util/async_queue_impl.ts
Line 186 in afe6e22
Steps to reproduce:
npx create-react-app my-project
myTest.test.js
firebase emulators:start
npm test
Relevant Code:
Test code:
First test pass and fail for the correct reason. Second test exit the Node process with the aforementioned error message.
Firestore rules
package.json dependencies
The text was updated successfully, but these errors were encountered: