我正在尝试使用酶来测试我的 React Native 项目,并按照设置说明进行操作。
https://github.com/airbnb/enzyme/blob/master/docs/guides/react-native.md
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "mocha --require react-native-mock/mock.js --compilers js:babel-core/register --recursive test/**/*.js"
},
这工作正常,我自己的代码被正确转译,但是当我包含一个不转译其代码的模块时(例如https://github.com/aksonov/react-native-router-flux),我的测试拒绝运行因为它在这些模块中的导入语句中出错。
我怎样才能让 babel 转译这些模块,或者还有其他方法可以让我的测试运行吗?
更新
似乎非转译的 3rd 方模块在 React Native 中相当普遍,因为 React Native 本身没有转译。
该解决方案似乎是强制转译和使用 react-native-mock 的组合。 https://github.com/facebook/react-native/issues/5392
但是,由于 NavigationExperimental 没有被嘲笑,我对 react-native-router-flux 有进一步的问题。
相关链接是:
https://github.com/lelandrichardson/react-native-mock/issues/23 https://github.com/lelandrichardson/react-native-mock/issues/22 https://github.com/lelandrichardson/react-本机模拟/拉/ 34
如果我找到解决方案,我会在这里更新。
更新 2
我在下面包含了我当前的解决方法,以防有人发现它有用。