通过将react-testing-library与react-native-testing-library合并来进行Web和移动跨平台测试

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

在使用react-native-web时,我尝试通过按平台解析文件扩展名来合并两个库以进行跨平台测试,这样我就可以为Web和移动设备编写1个测试。

它在大多数情况下都有效,但随着我的应用程序的增长,一些测试变得不稳定。通常我会发现异步问题,测试在 Web 环境中通过,但在移动设备上挂起,反之亦然。我正在建立一个新的跨平台项目,我想知道是否有人通过这种方法取得了长期成功?或者是当今为移动/网络编写 2 个测试的最可靠方法。或者还有其他工具吗?

方法示例:

test-utils/index.web.js

export * from @testing-library/react;

test-utils/index.js

export * from @testing-library/react-native;

App.test.jsx

import { render, waitFor } from './test-utils'

describe('Initial test', () => {
  test('App should render', async () => {
    const { findByTestId } = render(Component)
    const someViewInMyApp = findByTestId('some-id')
    waitFor(() => expect(someViewInMyApp).toBeTruthy())
  })
})

还有更多的事情要做,需要一些 api 的包装器,例如事件、用 Providers 包装的渲染方法或用于导航的 Route / Screen 等..

例如:

test-utils/events.web.js

import { fireEvent } from ‘@testing-library/react’

export const fireChangeText = (input, value) =>
  fireEvent.change(input, { target: { value } })

test-utils/events.js

import { fireEvent } from ‘@testing-library/react-native’

export const fireChangeText = (input, text) =>
  fireEvent(input, ‘onChangeText’, text)

some.test.jsx

import { fireChangeText } from './test-utils/events'
reactjs react-testing-library react-native-web react-native-testing-library
1个回答
0
投票

我有同样的问题,取得了初步成功,但现在面临问题

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