Skip to content

Support firebase-admin v14 #327

Description

@ChromeQ

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:

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:

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.

export function makeDocumentSnapshot(

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?

// 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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions