测试本机特定于React Native的扩展代码

测试本机特定于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$"
  }
 ...