firebase / firebase/firebase-js-sdk

Error: FIRESTORE (9.10.0) INTERNAL ASSERTION FAILED: Unexpected state when trying to run unit test for Firestore Rules with Firebase Emulators

Open
#6,684 5 comments 6 reactions 1 assignee Claimed by @MarkDuckworth View on GitHub
api: firestore needs-attention question stack:React testing-sdk
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### Describe your environment

* Operating System version: Ubuntu 22.04.1 LTS
* Node version: 16.15.0
* Firebase SDK version: 9.10.0
* Firebase Rules Unit Testing version: 2.0.4
* Firebase Product: Firestore
* Firebase Emulators version: 11.14.2

### 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:

> node:internal/process/promises:279
> triggerUncaughtException(err, true /* fromPromise */);
> ^
>
> Error: FIRESTORE (9.10.0) INTERNAL ASSERTION FAILED: Unexpected state

Stack trace points here: https://github.com/firebase/firebase-js-sdk/blob/afe6e22845f7b91f19bd9a6b8179764e95e27254/packages/firestore/src/util/async_queue_impl.ts#L186

#### Steps to reproduce:

1. create a new project with `npx create-react-app my-project`
2. create a Firebase project, and enable Firestore
3. Install Firebase Emulators with ǹpm install -g firebase-tools`
4. write some test using Jest, example in file named `myTest.test.js`
5. run Firebase Emulators with `firebase emulators:start`
6. run tests with `npm test`

#### Relevant Code:

##### Test code:

```javascript
describe('The Cloud Firestore Security Rules', () => {
let testEnv, unauthDb

beforeAll(async () => {
testEnv = await initializeTestEnvironment({
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 () => {
await testEnv.clearFirestore()
})

it('should not allow any unauthenticated user to write to the database', async () => {
const docRef = doc(unauthDb, '/any', 'docId')

await assertFails(setDoc(docRef, { data: 'test' }))
})

it('should not allow any unauthenticated user to view users\' data', async () => {
const collectionPath = 'users'
const docPath = 'userId'
await testEnv.withSecurityRulesDisabled(
(amdinCtx) => setDoc(doc(amdinCtx.firestore(), collectionPath, docPath), { uid: 'user_123' })
)
const docRef = doc(unauthDb, collectionPath, docPath)

await assertFails(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;
}
}
}
```

##### package.json dependencies

```
"dependencies": {
"@reduxjs/toolkit": "^1.8.5",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",
"firebase": "^9.10.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.0.2",
"react-scripts": "^5.0.1",
"web-vitals": "^2.1.4"
},
"devDependencies": {
"@firebase/rules-unit-testing": "^2.0.4"
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.