【发布时间】:2021-05-27 04:05:21
【问题描述】:
我正在使用 Jest 和模拟器测试 Firebase 功能,尽管这些测试可能来自竞争条件。我所说的 flakey 是指有时它们通过,有时它们不通过,即使在同一台机器上也是如此。
测试和函数用 TypeScript 编写,然后用 babel 进行转译。
示例测试/功能
注意:这只是其中一个 flakey 测试的示例。许多其他测试都是不稳定的。一个解决方案最好是不只是解决这种情况,而是解决一般问题。
测试
import { onProfilesWrite } from '../src/profiles/on-write'
import { initializeAdminApp } from '@firebase/rules-unit-testing'
const admin = initializeAdminApp({ projectId: 'projectId' }).firestore()
const wrappedFunction = testEnvironment.wrap(onProfilesWrite)
const profilePath = `profiles/${uid}`
const customerProfile = {
roles: ['customer'],
slug: 'slug',
image: 'image.png',
fullName: 'John Smith',
}
const publisherRoles = ['customer', 'publisher']
const publisherProfile = {
...customerProfile,
roles: publisherRoles,
}
const createChange = async (
before: Record<string, unknown> | undefined,
changes: Record<string, unknown>
) => {
const publisherStatsRef = admin.doc(profilePath)
if (before) await publisherStatsRef.set(before)
const beforeSnapshot = await publisherStatsRef.get()
await publisherStatsRef.set(changes, { merge: true })
const afterSnapshot = await publisherStatsRef.get()
return testEnvironment.makeChange(beforeSnapshot, afterSnapshot)
}
test('If user profile is created as a publisher, publisherDetails is created', async () => {
const change = await createChange(undefined, publisherProfile)
await wrappedFunction(change)
const snapshot = await admin.doc(`profileDetails/${uid}`).get()
const data = snapshot.data()
expect(data).toBeTruthy()
expect(data?.id).toBeTruthy()
expect(data?.slug).toBe(publisherProfile.slug)
expect(data?.profileImage).toBe(publisherProfile.image)
expect(data?.publisherName).toBe(publisherProfile.fullName)
expect(data?.music).toMatchObject([])
})
运行测试
firebase emulators:exec \"jest functions/__tests__ --detectOpenHandles\" --only firestore
输出
If user profile is created as a publisher, publisherDetails is created
expect(received).toBeTruthy()
Received: undefined
46 | const snapshot = await admin.doc(`profileDetails/${uid}`).get()
47 | const data = snapshot.data()
> 48 | expect(data).toBeTruthy()
| ^
49 | expect(data?.id).toBeTruthy()
50 | expect(data?.slug).toBe(publisherProfile.slug)
51 | expect(data?.profileImage).toBe(publisherProfile.image)
功能
import * as functions from 'firebase-functions'
// initializes the admin app, then exports admin.firestore
import { firestore } from '../admin'
const database = firestore()
const createPublisherId = async (): Promise<string> => {
let id = ''
const MAX_NUMBER = 1000000
while (id === '') {
const temporaryId = String(Math.ceil(Math.random() * MAX_NUMBER))
const snapshot = await firestore()
.collection('publisherDetails')
.where('sku', '==', temporaryId)
.limit(1)
.get()
if (snapshot.empty) id = temporaryId
}
return id
}
export const createPublisherDetails = async (
newData: firestore.DocumentData,
uid: string
): Promise<void> => {
const id = await createPublisherId()
await database.doc(`publisherDetails/${uid}`).set(
{
id,
slug: newData.slug,
publisherName: newData.fullName,
profileImage: newData.image,
music: [],
},
{ merge: true }
)
}
export const onProfilesWrite = functions.firestore.document('profiles/{uid}').onWrite(
async (change): Promise<void> => {
const { id: uid } = change.after
const oldData = change.before.data()
const newData = change.after.data()
if (
newData?.roles?.includes('publisher') &&
(typeof oldData === 'undefined' || !oldData.roles?.includes('publisher'))
)
await createPublisherDetails(newData, uid)
}
)
调试步骤
- 所有承诺都在云函数中等待(由 ESLint 规则
@typescript-eslint/no-floating-promises确认) - 还将测试转换为 Mocha(按照 Firebase 文档的建议),同样的错误
- 将测试中的 async/await 转换为 promise.then() 语法
元数据
- 操作系统:macOS 11.2、Ubuntu 18.04
- 开玩笑:26.6.3
- Firebase:8.2.6
- Firebase 工具:9.3.0
随着 cmets 的加入,无论是问题还是建议,我都会继续更新这篇文章。
【问题讨论】:
-
不熟悉 jest,但是您的测试中在哪里设置了
uid?除非有某种神奇的注入,否则看起来您正在请求profileDetails/undefined并且它只是返回undefined。 -
Jest 测试默认并行运行。请尝试随后使用 --runInBand jest 参数运行它们。
标签: node.js firebase jestjs google-cloud-functions es6-promise