在使用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'
我有同样的问题,取得了初步成功,但现在面临问题