【问题标题】:Firebase Cloud Function unit test HTTP onCallFirebase Cloud Function 单元测试 HTTP onCall
【发布时间】:2020-08-25 11:04:05
【问题描述】:

我想为一堆云功能编写一些单元测试。现在我面临以下问题。使用 firebase-functions-test 我无法测试 HTTP 触发的云功能。以下是我想使用jest 测试的一些云功能:

export cloudFunctions = {
    createUserByAdmin: functions.runWith({ timeoutSeconds: 30, memory: '256MB' }).https.onCall(UserService.createUserByAdmin),
    updateUserByAdmin: functions.runWith({ timeoutSeconds: 30, memory: '256MB' }).https.onCall(UserService.updateUserByAdmin),
    deleteUserByAdmin: functions.runWith({ timeoutSeconds: 30, memory: '256MB' }).https.onCall(UserService.deleteUserByAdmin)
}

它们都部署在 Firebase 上,并且可以正常工作。但我找不到使用firebase-functions-test 包调用的方法。此外,还有一些如何使用该包编写单元测试的示例,但没有一个测试 http 触发函数。

这是我的测试文件:

import * as functions from 'firebase-functions-test'
import * as admin from 'firebase-admin'
import * as path from 'path'

const projectConfig = {
    projectId: 'test-fb',
}

const testEnv = functions(
    projectConfig,
    path.resolve('DO-NOT-EDIT.dev.fb-admin-sdk-key.json'),
)

describe('[Cloud Functions] User Service', () => {
    let cloudFunctions

    beforeAll(() => {
        cloudFunctions = require('../index')
    })

    afterAll(() => {
      // delete made accounts/entrys
    })

    describe('Testing "createUserByAdmin"', () => {
        it('Creating User does work', () => {
            expect(1).toBe(0)
        })
    })
})

有人知道如何测试http云功能吗?我真的很感激一个例子。 谢谢!

【问题讨论】:

    标签: firebase unit-testing jestjs google-cloud-functions


    【解决方案1】:

    我实际上找到了一种使用firebase-functions-test 测试HTTP Cloud Functions 的方法,它都可以与包装函数一起使用。看看这个reference page。这里有一些代码可以让事情更清楚。

    这是我的一个测试中的 sn-p

    import * as functions from 'firebase-functions-test'
    import * as admin from 'firebase-admin'
    import * as path from 'path'
    
    const projectConfig = {
        projectId: 'myproject-id',
    }
    
    const testEnv = functions(
        projectConfig,
        path.resolve('fb-admin-sdk-key.json'),
    )
    // has to be after initializing functions
    import * as cloudFunctions from '../index'
    
    describe('Testing "createUserByAdmin"', () => {
            const createUserByAdmin = testEnv.wrap(cloudFunctions.createUserByAdmin)
    
            it('Creating User does work', async (done) => {
                const data = {
                    displayName: 'Jest Unit Test',
                    email: 'unit@domain.com',
                    password: 'password',
                    uid: null,
                }
                const context = {
                    auth: {
                        token: {
                            access: 'somestring,
                        },
                        uid: 'mockuiddddd',
                    },
                }
                await createUserByAdmin(data, context)
                    .then(async (createdUser: any) => {
                        expect(createdUser.status).toBe('OK')
                        done()
                    })
                    .catch((error: any) => {
                        fail('createUserByAdmin failed with the following ' + error)
                    })
            })
        })
    

    在使用我们的projectConfig 和我们的服务帐户密钥文件初始化我们的测试环境后,您会看到。

    const testEnv = functions(
        projectConfig,
        path.resolve('fb-admin-sdk-key.json'),
    )
    

    您只需使用 .wrap() 函数包装适当的云函数。

    const createUserByAdmin = testEnv.wrap(cloudFunctions.createUserByAdmin)

    您可以像其他所有函数一样调用它(请记住,云函数通常需要一个数据参数(以及您在云函数中使用的变量)以及一个上下文参数,具体取决于您如何处理身份验证/授权您必须尝试并出错才能找到您的函数请求的正确上下文属性。

    如果您为生产云功能编写测试确保在运行测试后进行清理 - 例如删除创建的帐户或删除 firestore 或 realtime-database 中的数据

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 2019-02-22
      • 1970-01-01
      • 2021-04-26
      • 2021-07-17
      • 2019-01-26
      • 2021-08-15
      • 2019-07-03
      • 2022-01-20
      相关资源
      最近更新 更多