Gen2 / TypeScript: Snapshot from 'makeDocumentSnapshot' Doesn't Work for 'onDocumentCreated'
I maintainer di solito rispondono entro 8 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 48/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Tranquilla
- Stack tecnologico
- typescript
- Ambito
- backend, testing-qa
Direzione di ricerca
Inizia confrontando firestore.makeDocumentSnapshot con la struttura dell’evento prevista da onDocumentCreated, quindi traccia come wrap passa lo snapshot alla funzione. Riproduci il fallimento con il caso di test TypeScript e conferma done quando lo snapshot può essere passato direttamente, event.data().data() è leggibile e l’asserzione sul risultato in maiuscolo ha esito positivo.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Version info
firebase-functions-test: 3.1.0
firebase-functions: 4.4.1
firebase-admin: 11.9.0
Test case
Firebase Cloud Function
import { onDocumentCreated } from 'firebase-functions/v2/firestore';
import * as logger from 'firebase-functions/logger';
import { initializeApp } from 'firebase-admin/app';
initializeApp();
export const docCreateTest = onDocumentCreated(
'messages/{documentId}',
(event) => {
const original = event.data?.data().original;
logger.log('Uppercasing: ', original);
const uppercase = original.toUpperCase();
return event.data.ref.set({ uppercase }, { merge: true });
}
);
Unit Test
Note: Using Vitest but I believe it would be the same result with Jest, etc.
import { describe, test, expect } from 'vitest';
import firebaseFunctionsTest from 'firebase-functions-test';
import * as admin from 'firebase-admin';
import { docCreateTest } from '../functions/index';
// NOTE: Don't need the service account file when using
// GOOGLE_APPLICATION_CREDENTIALS environment variable
const { wrap, firestore } = firebaseFunctionsTest({
databaseURL: 'http://127.0.0.1:8080',
projectId: 'project-id-test',
});
describe('Running tests...', () => {
test('should return uppercase message', async () => {
// create mock doc snapshot
const snap = firestore.makeDocumentSnapshot(
{ original: 'hello' },
'messages/abc'
);
// TODO: Workaround for https://github.com/firebase/firebase-functions-test/issues/163
// @ts-ignore
const wrapped = wrap(docCreateTest);
await wrapped(snap);
const result = await admin.firestore().doc('messages/abc').get();
expect(result.data()?.uppercase).toBe('HELLO');
});
});
Steps to reproduce
Run the unit test against the Firebase function and it will result in a Cannot read properties of undefined (reading 'original') when trying to create the 'original' variable in the function (const original = event.data?.data().original;).
This is because event.data.data() is undefined.
Expected behavior
The document snapshot created in the unit test with makeDocumentSnapshot is in the correct format to be passed into onDocumentCreated so that its data can be read.
Actual behavior
The 'original' variable cannot be created in the Firebase function because it can't read the data object in the passed in event resulting in the following error...
Cannot read properties of undefined (reading 'original')
I guess this is due to makeDocumentSnapshot creating a DocumentSnapshot and the event in onDocumentCreated expected to have a type of FirestoreEvent<QueryDocumentSnapshot | undefined, { documentId: string; }>.
I can't find any alternative to makeDocumentSnapshot that should be used in this scenario for gen2 functions though so I'm guessing this isn't supported yet?
If I create an object that contains the document snapshot as the value of a data parameter, it will work
test('should return uppercase message', async () => {
// create mock doc snap
const snap = firestore.makeDocumentSnapshot(
{ original: 'hello' },
'messages/abc'
);
// TODO: My workaround for missing 'data' property when passing event
const event = { data: snap }; // <-- CREATING THE NEW OBJECT
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const wrapped = wrap(docCreateTest);
await wrapped(event); // <-- PASSING THE OBJECT I CREATED INSTEAD OF 'snap'
const result = await admin.firestore().doc('messages/abc').get();
expect(result.data()?.uppercase).toBe('HELLO');
});
I'm not sure how much of this library is currently expected to work with the gen2 Firebase functions, but I couldn't find this issue already logged and the gen2 samples don't cover onDocumentCreated functions.
Cheers
Jess
- Lingua principale
- TypeScript
- Stelle
- 249
- Fork
- 63
- Merge medio
- 7g 19h
- PR unite (30g)
- 2
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di firebase/firebase-functions-test
-
Pending breaking changesAperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
firebase/firebase-functions-test#347 ·
I maintainer di solito rispondono entro 8 giorni
-
type: enhancement
Difficoltà 3/5 1-2 giorni Idoneità per principianti 58/100
firebase/firebase-functions-test#333 · 2 commenti ·
I maintainer di solito rispondono entro 8 giorni
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
firebase/firebase-functions-test#325 · 3 commenti · 3 reazioni ·
I maintainer di solito rispondono entro 8 giorni
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
firebase/firebase-functions-test#281 · 2 commenti ·
I maintainer di solito rispondono entro 8 giorni
-
triaged type: feature request
Difficoltà 2/5 1-3 ore Idoneità per principianti 52/100
firebase/firebase-functions-test#218 · 2 commenti ·
I maintainer di solito rispondono entro 8 giorni
Tutte le issue di firebase/firebase-functions-test
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
scaleway/ultraviolet#7089 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 67/100
tencent-connect/dsh-qqbot#81 ·
I maintainer di solito rispondono entro 1 giorno
-
[Bug]: Pen taps on sidebar rows start a drag and lose their clickForse già presa @makhnatkin l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
clangd/vscode-clangd#1009 ·