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
Hi, I am trying to setup firestore rules unit tests.
Since I am using typescript and jest for testing, I have edited your provided example a bit. This is what I have currently:
import{readFileSync}from'fs-extra'import*asfirebasefrom'@firebase/rules-unit-testing'import{getDoc,setDoc,setLogLevel}from'firebase/firestore'describe(' Firestore Rules',()=>{lettestEnv: firebase.RulesTestEnvironment;beforeAll(async()=>{testEnv=awaitfirebase.initializeTestEnvironment({firestore: {rules: readFileSync('../firestore.rules','utf8')},projectId: 'podsuite',hub: {host: 'localhost',port: 4000},});});afterAll(async()=>{awaittestEnv.cleanup();});beforeEach(async()=>{awaittestEnv.clearFirestore();});test('noone should have access to user document',asyncfunction(){// Setup: Create documents in DB for testing (bypassing Security Rules).awaittestEnv.withSecurityRulesDisabled(async(context)=>{consttestdoc=context.firestore().collection('users').doc('user1234');awaitsetDoc(testdoc,{test: 'test1'});});constunauthedDb=testEnv.unauthenticatedContext().firestore();// Then test security rules by trying to read it using the client SDK.consttestdoc=unauthedDb.collection('users').doc('user1234');awaitfirebase.assertFails(getDoc(testdoc));});});
My emulator runs on http://localhost:4000/
These are my rules:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, write: if false;
}
match /users/{userId}/salesItems/{documentId} {
allow read: if true;
allow write: if false;
}
match /users/{userId}/overviews/{documentId} {
allow read: if true;
allow write: if false;
}
}
}
When I run npm test I am getting an error :
FAIL src/tests/rules.test.ts
● Firestore Rules › noone should have access to user document
FetchError: invalid json response body at http://localhost:4000/emulators reason: Unexpected token < in JSON at position 0
at node_modules/@firebase/rules-unit-testing/node_modules/node-fetch/lib/index.js:272:32
What is wrong?
The text was updated successfully, but these errors were encountered:
I received the same error message while writing my test suite.
When initializing the test environment you set the hub port to 4000. This is the UI port. You have to pass the port of the emulator hub (by default 4400).
Changing your code to the following should allow the tests to connect to the emulators:
Hi, I am trying to setup firestore rules unit tests.
Since I am using typescript and jest for testing, I have edited your provided example a bit. This is what I have currently:
My emulator runs on
http://localhost:4000/
These are my rules:
When I run npm test I am getting an error :
What is wrong?
The text was updated successfully, but these errors were encountered: