firebase / firebase/quickstart-testing

Testing firestore rules against emulator fails with reason: Unexpected token < in JSON at position 0

Offen
#225 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
JavaScript
Sterne
368
Forks
125
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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:

```ts
import { readFileSync } from 'fs-extra'
import * as firebase from '@firebase/rules-unit-testing'
import { getDoc, setDoc, setLogLevel } from 'firebase/firestore'

describe(' Firestore Rules', () => {
let testEnv: firebase.RulesTestEnvironment;

beforeAll(async () => {
testEnv = await firebase.initializeTestEnvironment({
firestore: { rules: readFileSync('../firestore.rules', 'utf8') },
projectId: 'podsuite',
hub: { host: 'localhost', port: 4000 },
});
});

afterAll(async () => {
await testEnv.cleanup();
});

beforeEach(async () => {
await testEnv.clearFirestore();
});

test('noone should have access to user document', async function () {
// Setup: Create documents in DB for testing (bypassing Security Rules).
await testEnv.withSecurityRulesDisabled(async (context) => {
const testdoc = context.firestore().collection('users').doc('user1234');
await setDoc(testdoc, { test: 'test1' });
});

const unauthedDb = testEnv.unauthenticatedContext().firestore();

// Then test security rules by trying to read it using the client SDK.
const testdoc = unauthedDb.collection('users').doc('user1234');

await firebase.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?

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit src/tests/rules.test.ts und dem Setup von initializeTestEnvironment, und vergleiche dann den konfigurierten Hub unter localhost:4000 mit dem verwendeten Emulator-Endpunkt. Führe npm test aus und untersuche die Antwort von /emulators. Erledigt ist es, wenn der Firestore-Regeltest die Emulatorverbindung startet und ohne den Fehler „invalid JSON“ abgeschlossen wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
databases, testing
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.