测试本机特定于React Native的扩展代码 [英] Test platform specific extension code for React Native

查看:106
本文介绍了测试本机特定于React Native的扩展代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,React Native为正在运行的平台动态地需要一个带有特定文件扩展名*.ios.js*.android.js的文件.但是,在测试环境中运行此代码时,会出现require错误,因为在文件树中找不到模块require('./module'),如下所示:

Currently, React Native dynamically requires a file for the running platform with an specific file extension, *.ios.js or *.android.js. However, when running this code inside a test environment, we get a require error because the module require('./module') cannot be found in the file tree, which looks like:

  • module.ios.js
  • module.android.js
  • module.ios.js
  • module.android.js

我们如何在测试环境中处理此问题?

How can we handle this issues in a test environment?

推荐答案

moduleFileExtensions中添加特定于平台的扩展,从而通过jest.14.1解决了我的问题.感谢: github链接

Adding platform specific extensions to moduleFileExtensions fixed the problem for me with jest.14.1. Credits to : github link

这是示例package.json中的代码片段,它将运行包括scene.jest.js(x)在内的所有文件.

Here is a snippet from sample package.json which will run all files including scene.jest.js(x).

...
"jest": {
    "verbose": true,
    "preset": "jest-react-native",
    "moduleFileExtensions": ["js", "android.js", "ios.js"],
    "testRegex": "\\.scene\\.jest\\.jsx$"
  }
 ...

这篇关于测试本机特定于React Native的扩展代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆