我在React原生项目中使用Detox来运行端到端测试.我也在使用伪装者.js模拟我的API请求,我正在努力找到一种方法来知道应用程序当前是否处于"测试"模式.

如果我使用mock shim.js来传递我们的请求,那么我应该告诉env shim.js.

有没有办法告诉Detox发布了这个应用&是否从JS内部运行测试?理想情况下,我会在测试时寻找某种类型的变量集,或者从命令行(TESTING=true react-native start__TESTING__)传递过来的东西

推荐答案

try 使用react-native-config.这里还有一篇关于Managing Configuration in React Native与react native config的好文章.

我在这里给出了一个在测试过程中如何使用配置来禁用animated-button-block-the-detox动画的例子.

基本的 idea 是你创造.用于所有不同构建环境(开发、生产、测试等)的环境配置文件.它们保存了您可以从中访问的配置变量 Javascript、Objective-C/Swift或Java.

然后指定哪个.构建应用程序时要使用的环境配置文件:

$ ENVFILE=.env.staging react-native run-ios # bash

这是包装的一个例子.Detock使用的json文件.用于构建应用程序的环境配置文件.

"detox": {
  "specs": "e2e",
  "configurations": {
    "ios.sim.release": {
      "binaryPath": "ios/build/Build/Products/Release-iphonesimulator/example.app",
      "build": "ENVFILE=.env.production export RCT_NO_LAUNCH_PACKAGER=true && xcodebuild -project ios/example.xcodeproj -scheme example -configuration Release -sdk iphonesimulator -derivedDataPath ios/build",
      "type": "ios.simulator",
      "name": "iPhone 5s, iOS 10.3"
    },
    "ios.sim.test": {
      "binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/example.app",
      "build": "ENVFILE=.env.testing xcodebuild -project ios/example.xcodeproj -scheme example -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build -arch x86_64",
      "type": "ios.simulator",
      "name": "iPhone 5s, iOS 10.3"
    }
  }
}

React-native相关问答推荐

React native React native下拉 Select 器下拉列表不透明问题

哪个版本的@stripe/stripe-react-native 模块支持 react native 0.62.0 typscript 模板?

React Native 无法下载模板

是否有用于react-native 的简单同步存储选项?

React Native 获取视图相对于屏幕的绝对位置

React Native - 如何判断 UI/元素?

如何在 React Native cli 中获取 SHA-1 密钥?

Xcode 12 问题:Build input file cannot be found

React-Native 构建失败的应用程序:mergeDebugAssets

当我导航到react-native 的新页面时,如何强制卸载组件?

使用带有样式组件的动画

动画属性启动时 ReactNative ActivityIndi​​cator 不显示 false

使用 expo SDK react-native ios 应用程序的 Info.plist 文件

React Native Image 不显示,我该如何解决?

如何从 React-Native 中的另一个类调用函数?

使用 React Native 运行 Firebase 3.0 时出错

Mapping children of a parent component

加载字体native-base 错误

/bin/sh: adb: 找不到命令

React Native:任务':app:transformDexArchiveWithExternalLibsDexMergerForDebug'的执行失败