firebase / firebase/firebase-js-sdk

`withFunctionTriggersDisabled` from `@firebase/rules-unit-testing` fails with timeout if there is no internet connection

Open
#8,022 9 comments 0 reactions 0 assignees View on GitHub
api: functions needs-attention question testing-sdk
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### Operating System

Windows

### Browser Version

-

### Firebase SDK Version

10.8.0

### Firebase SDK Product:

Firestore

### Describe your project's tooling

Node JS Cloud Functions accessing firestore

### Describe the problem

When using the emulator suite and using `@firebase/rules-unit-testing` to test Firestore rules, as soon as I add the following code in an `after` hook in the tests and execute the tests **while being offline**, they fail with a timeout:
```typescript
...
let adminDb: Firestore
let firebaseTestEnvironment: RulesTestEnvironment
let userDB: firebaseJSCompat.firestore.Firestore

before(async () => {
adminDb = getFirestore()

const rulesContent = fs.readFileSync(path.resolve(__dirname, '../../../firestore.rules'), 'utf8')

firebaseTestEnvironment = await initializeTestEnvironment({
projectId: TEST_FIREBASE_PROJECT_ID,
firestore: {
rules: rulesContent,
},
})

userDB = firebaseTestEnvironment
.authenticatedContext("123", {
email: `whatever@example.com`,
})
.firestore()
})

it('tests something', async () => {
await assertSucceeds(inexistentUserDB.collection("whatever").doc("abc").get())
})
...

after(async () => {
await resetData()
})

async function resetData(): Promise {
await withFunctionTriggersDisabled(async () => {
// For testing purposes we do nothing here as it doesn't matter
await Promise.resolve()
})
}
```
The error logged is:
```
"after all" hook for "tests something":
Error: Timeout of 15000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
```

**When getting back online without changing any code, everything passes and there is no timeout.**
The same happens if the `withFunctionTriggersDisabled` is removed.

The tests are executed using
```
firebase emulators:exec --import=./test/emulatordata \"npm test\"
```

`npm test` is `mocha --reporter spec --timeout 15000`.

### Steps and code to reproduce issue

See above.

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.