firebase / firebase/firebase-functions-test
Support firebase-admin v14
- Dominant language
- TypeScript
- Stars
- 248
- Forks
- 63
- PR merge metrics
- No merged PRs in 30d
Description
Since firebase-admin was updated to v14 then my tests have been throwing a type error with this package.
Here is a simple reproduction test:
```ts
import { onDocumentWritten } from 'firebase-functions/firestore';
import functions from 'firebase-functions-test';
import { getFirestore } from 'firebase-admin/firestore';
const testHandler = onDocumentWritten(
{
document: 'test-col/{id}',
},
(event) => {
const before = event.data?.before.data();
const after = event.data?.after.data();
console.log('Before:', before);
console.log('After:', after);
}
);
it('should run this test', async () => {
const wrapped = functions().wrap(testHandler);
const ref = getFirestore().collection('test-col').doc('test-id');
await ref.set({ foo: 'bar' });
const beforeSnap = testEnv.firestore.makeDocumentSnapshot({}, ref.path); // 👈 error line
const afterSnap = await ref.get();
const change = testEnv.makeChange(beforeSnap, afterSnap);
await wrapped({ data: change });
});
```
This is the error thrown when running the test:
```sh
TypeError: (0 , firebase_admin_1.firestore) is not a function
❯ Object.makeDocumentSnapshot ../node_modules/.pnpm/firebase-functions-test@3.5.0_firebase-admin@14.1.0_firebase-functions@7.2.5_firebase-a_0f4ad8eb00077a7969708a21d93a6210/node_modules/firebase-functions-test/lib/providers/firestore.js:59:59
```
---
### Additional Note:
Whilst fixing this, would it be possible to correctly type the return type of `makeDocumentSnapshot` rather than `any`? Very strict TS projects don't like `any`.
https://github.com/firebase/firebase-functions-test/blob/e74c0b58c0c07b3ea142660d191c1a96ea0dc200/src/providers/firestore.ts#L68
The declaration file ends up with `any` despite the `exampleDocumentSnapshot` returning `firestore.DocumentSnapshot` type when it just calls `makeDocumentSnapshot` - so `makeDocumentSnapshot` should also return `firestore.DocumentSnapshot` right?
```.d.ts
// node_modules/firebase-functions-test/lib/providers/firestore.d.ts#16
/** Create a DocumentSnapshot. */
export declare function makeDocumentSnapshot(
/** Key-value pairs representing data in the document, pass in `{}` to mock the snapshot of
* a document that doesn't exist.
*/
data: {
[key: string]: any;
},
/** Full path of the reference (e.g. 'users/alovelace') */
refPath: string, options?: DocumentSnapshotOptions): any;
/** Fetch an example document snapshot already populated with data. Can be passed into a wrapped
* Firestore onCreate or onDelete function.
*/
export declare function exampleDocumentSnapshot(): firestore.DocumentSnapshot;
```
Contributor guide
Research direction
Start with src/providers/firestore.ts, especially makeDocumentSnapshot and the declaration location linked in the issue, then run the provided reproduction with firebase-admin v14. Done means the Firestore test no longer throws the reported runtime TypeError and the generated declaration gives makeDocumentSnapshot the appropriate DocumentSnapshot return type instead of any.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- databases, testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 72/100