Ask HN: 在设备上运行 React Native 测试,我该用什么?
3 分•作者: dboreham•9 个月前
在 JavaScript/TypeScript 领域,我发现一个常见的问题是,单元测试通常需要在 Node.js 进程中运行。过去在各种项目中,这种方法被证明是有问题的,因为无法实现与目标运行时环境(浏览器)的“环境一致性”(例如,没有 WebRTC[1])。在那些项目中,我们通过编写自己的测试运行器来解决这个问题,该运行器可以被注入到无头浏览器进程中。
现在,在使用 React Native 的移动设备项目中,也出现了类似的问题:我原以为单元测试可以在设备或至少模拟器上运行,但事实似乎并非如此(参见:https://docs.expo.dev/develop/unit-testing/)。或者至少到目前为止,我还没有找到实现这一目标的秘诀或工具。欢迎提供任何建议。
[1] 是的,有一个针对 Node 的 WebRTC 端口,它使用旧版本的 Google 的 C++ 库,但它实际上无法正常工作。
查看原文
Something of a common problem in JS/TS-land, I've found is that unit tests are expected to be run in a Node.js process. In the past in various projects this approach proved problematic because "environment parity" with the target runtime (browser) couldn't be achieved (e.g. no WebRTC[1]). In those projects we resolved this problem by writing our own test runner that could be injected into a headless browser process.<p>So now the same sort of problem has arisen with a mobile device project using react native: I assumed that unit tests would be runnable on a device or at least an emulator, but it seems not (see: https://docs.expo.dev/develop/unit-testing/). Or at least so far I haven't found the secret sauce or tool to allow that. Any pointers welcome.<p>[1]Yes there is a WebRTC port for Node that uses an old version of Google's C++ library, but it doesn't actually work properly.