使用 Jest 测试 firebase 功能时的 Flakey 测试

问题描述 投票:0回答:1

我正在使用 Jest 和模拟器测试 Firebase 功能,尽管测试可能因竞争条件而不稳定。我所说的“flakey”是指有时它们会通过,有时则不会,即使在同一台机器上也是如此。

测试和函数是用 TypeScript 编写的,然后用 babel 进行编译。

示例测试/功能

注意:这只是其中一项不稳定测试的示例。许多其他测试都是不稳定的。解决方案最好不仅能解决这一案例,还能解决一般问题。

测试

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

随着评论的不断涌现,无论是问题还是建议,我将继续更新这篇文章。

node.js firebase jestjs google-cloud-functions es6-promise
1个回答
1
投票

将您的测试部分更改为如下:

test('If user profile is created as a publisher, publisherDetails is created', async () => {
  const change = await createChange(undefined, publisherProfile);
  await wrappedFunction(change);
  const documentObject = await admin.doc(`profileDetails/${uid}`);
  const snapshot = await documentObject.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([]);
});

原因是在您的测试区域中,您对await的使用有点不正确(正在等待的对象上的函数链在同一调用行中是一个很大的禁忌)

© www.soinside.com 2019 - 2024. All rights reserved.