You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用firebase-functions-test测试onDocumentCreated时data和params未定义

问题描述

我在Firebase Functions的index.ts里写了一个简单的onDocumentCreated函数,仅打印data和params,代码如下:

import { initializeApp } from 'firebase-admin/app'
import { onDocumentCreated } from 'firebase-functions/v2/firestore'

initializeApp()

exports.onNewSale = onDocumentCreated('customers/{customerId}/sales/{saleId}', (event) => {
  console.log('params', event.params)
  console.log('data', event.data?.data())
})

在Firebase模拟器中创建文档触发函数时,日志输出正常:

i  functions: Beginning execution of "us-central1-onNewSale"
>  params { customerId: '3s2dwWr2QdmmZlze788Z', saleId: '62OUh9iqX0BmONfVCohJ' }
>  data { foo: 'bar' }

但使用firebase-functions-test结合Mocha测试时,data返回undefined,params为空对象,测试输出如下:

$ mocha --require ts-node/register --reporter spec


  onNewSale
    execution
params {}
data undefined
      ✔ should execute


  1 passing (6ms)

✨  Done in 1.56s.

我的测试代码index.test.js如下:

const test = require('firebase-functions-test')(
  {
    databaseURL: 'https://my-project.firebaseio.com',
    projectId: 'my-project',
    storageBucket: 'my-project.appspot.com'
  },
  'serviceAccountKey.json'
)

const { onNewSale } = require('../src/index.ts')

describe('onNewSale', function () {
  describe('execution', function () {
    it('should execute', function (done) {
      const data = test.firestore.makeDocumentSnapshot({ foo: 'bar' })

      const params = {
        customerId: '3s2dwWr2QdmmZlze788Z',
        saleId: '62OUh9iqX0BmONfVCohJ'
      }

      const wrapped = test.wrap(onNewSale)

      wrapped(data, { params })

      done()
    })
  })
})

请问我哪里操作出错了?


问题原因与修正方案

你的问题出在V2版本Firebase Functions的测试参数格式和V1完全不同,不能沿用V1的传参逻辑。

具体错误点:

  1. 参数传递结构错误:V2的onDocumentCreated接收的是一个完整的事件对象,而非分开的data和params两个独立参数。你当前的传参方式不符合V2的事件结构要求。
  2. 文档快照包装错误:V2的Firestore事件通过data.after存储文档创建后的快照(onDocumentCreated事件只有after有值),而非直接传入快照对象。

修正后的测试代码:

const test = require('firebase-functions-test')(
  {
    databaseURL: 'https://my-project.firebaseio.com',
    projectId: 'my-project',
    storageBucket: 'my-project.appspot.com'
  },
  'serviceAccountKey.json'
)

const { onNewSale } = require('../src/index.ts')

describe('onNewSale', function () {
  describe('execution', function () {
    it('should execute', function (done) {
      // 创建文档创建后的快照
      const afterSnapshot = test.firestore.makeDocumentSnapshot({ foo: 'bar' })
      
      const params = {
        customerId: '3s2dwWr2QdmmZlze788Z',
        saleId: '62OUh9iqX0BmONfVCohJ'
      }

      const wrapped = test.wrap(onNewSale)

      // V2需传入完整事件对象:data包含after快照,params直接挂载在对象上
      wrapped({
        data: { after: afterSnapshot },
        params: params
      })

      done()
    })
  })
})

额外说明:

  • V2 Firestore事件的通用结构:事件对象包含data(含before和after快照,对应文档变更前后状态)、params、timestamp等字段,和V1的change对象结构差异极大。
  • 若测试onDocumentUpdated事件,需同时传入before和after快照;测试onDocumentDeleted事件则仅需传入before快照。

内容的提问来源于stack exchange,提问作者Marcel Otten

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 10:25:55