firebase / firebase/firebase-js-sdk

`transaction.get` does not match firestore-admin in @firebase/testing

Open
#2,535 6 comments 3 reactions 1 assignee Claimed by @yuchenshi View on GitHub
api: firestore bug testing-sdk
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### [REQUIRED] Describe your environment

* Operating System version: Ubuntu 19.10
* Browser version: N/A
* Firebase SDK version: 7.6.2
* Firebase Product: testing / firestore

### [REQUIRED] Describe the problem
With an app running on cloud functions the firestore is accessed via `firebase-admin` initializeApp.
For testing purposes I import `@firebase/testing` and initializeAdminApp to create a firestore.

In production code the code below works, but in testing it throws an error:
```> (node:23599) UnhandledPromiseRejectionWarning: FirebaseError: Function Transaction.get() requires its first argument to be a DocumentReference, but it was: a custom CollectionReference object```

#### Relevant Code:
Here is a simple snippet of example;
```javascript
if (process.env.NODE_ENV !== 'test') {
admin = require('firebase-admin');

admin.initializeApp();
} else {
const firebase = require('@firebase/testing');

admin = firebase.initializeAdminApp({
projectId: 'sample_app'
});
}

const firestore = admin.firestore();

const query = firestore.collection('foo');

const transaction = firestore.runTransaction(async transaction => {
const col = await transaction.get(query);

console.log(col.docs);
});
```
Now run this code with `npx firebase emulators:start --only functions` to see it working as expected.
Run again with `NODE_ENV=test npx firebase emulators:start --only functions` to see the error

Because @firebase/testing uses the firebase (client) package to emulate the admin SDK the Transaction methods are not the same as in the admin SDK. This makes writing my tests obsolete as I cannot replicate production process in my test env.

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.