如何判断 Detox 正在运行测试? [英] How to tell Detox is running tests?

查看:36
本文介绍了如何判断 Detox 正在运行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Detox to run end to end tests in my React Native project. I'm also using pretender.js to mock my API requests and I'm struggling to find a way to know if the app is currently in "testing" mode.

我传递了一个 env 变量(并使用 babel-transform-inline-environment-variables)来判断我是否应该模拟请求,但这会破坏 shim.js 在我们的发布版本中.

I was passing an env variable down (and using babel-transform-inline-environment-variables) to tell if I should mock the requests but that breaks shim.js in our release builds.

有什么办法可以告诉 Detox 启动了应用程序吗?正在从 JS 内部运行测试?理想情况下,我正在寻找在测试时设置的某种变量或从命令行传递的东西(TESTING=true react-native start__TESTING__)

Is there any way to tell Detox launched the app & is running tests from within the JS? Ideally I'm looking for some sort of variable set at test time or something passed down from the command line (TESTING=true react-native start or __TESTING__)

推荐答案

尝试使用 react-native-配置.这里也是一篇关于在 React Native 中管理配置的好文章react-native-config.

Try using react-native-config. Here is also a good article on Managing Configuration in React Native with react-native-config.

我也在这里给出了答案animated-button-block-the-detox带有如何在测试期间使用 react-native-config 禁用循环动画​​的工作示例.

I also gave an answer here animated-button-block-the-detox with working example of how react-native-config can be used to disable looping animations during testing.

基本思想是为所有不同的构建环境(开发、生产、测试​​等)创建 .env 配置文件.这些包含您可以从 Javascript、Objective-C/Swift 或Java 访问的配置变量.

The basic idea is that you create .env config files for all your different build environments (development, production, test, etc). These hold your configuration variables that you can access from either Javascript, Objective-C/Swift, or Java.

然后指定在构建应用程序时使用哪个 .env 配置文件:

You then specify which .env config file to use when building your app:

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

这是 package.json 文件的一个示例,其中 detox 使用 .env 配置文件来构建应用程序.

And this is an example of package.json file where detox uses .env config files for building the app.

"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"
    }
  }
}

这篇关于如何判断 Detox 正在运行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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