我使用排毒在Reactive原住民项目中运行端到端的测试。我也在使用pretender.js来模拟我的API请求,我很难找到一种方法来知道这个应用程序目前是否处于“测试”模式。
我向下传递了一个env变量(并使用babel-transform-inline-environment-variables),以判断是否应该模拟请求,但这会破坏发行版构建中的shim.js。
有没有办法告诉Detox发布了这款应用&是否在JS内部运行测试?理想情况下,我正在寻找在测试时设置的某种变量,或者从命令行(TESTING=true react-native start或__TESTING__)传递下来的东西。
发布于 2017-12-15 03:01:08
尝试使用react本机-config。这里还有一篇很好的文章,介绍了在React本机中管理配置与react本机配置的关系。
我还在这里给出了一个答案,动画-按钮-阻止-排毒给出了如何在测试期间使用react本机-config来禁用循环动画的工作示例。
基本思想是为所有不同的构建环境(开发、生产、测试等)创建.env配置文件。这些文件保存了您可以从 ( Javascript、Objective/Swift或Java )访问的配置变量。
然后指定在构建应用程序时使用哪个.env配置文件:
$ ENVFILE=.env.staging react-native run-ios # bash
这是一个package.json文件的例子,在该文件中,detox使用.env配置文件构建应用程序。
"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"
}
}
}发布于 2018-05-04 02:06:05
我们利用了这样一个事实: detox在iOS命令行上用iOS调用二进制文件,在InstrumentationRegistry中调用{ detoxServer: ..., detoxSessionId: ... }集。
我们目前向JS公开这个问题的方式对于StackOverflow答案来说有点过分,但是下面是一些示例代码,这些代码以及react本地文档应该可以帮助您达到这个目的--用于Android:
// This will throw ClassNotFoundException if not running under any test,
// but it still might not be running under Detox
Class<?> instrumentationRegistry = Class.forName("android.support.test.InstrumentationRegistry");
Method getArguments = instrumentationRegistry.getMethod("getArguments");
Bundle argumentsBundle = (Bundle) getArguments.invoke(null);
// Say you're in your BaseJavaModule.getConstants() implementation:
return Collections.<String, Object>singletonMap("isDetox", null != argumentsBundle.getString("detoxServer"));在iOS上,类似于(没有目标C编译器的ATM):
return @{@"isDetox": [[[NSProcessInfo processInfo] arguments] containsObject: @"-detoxServer"]}注意,还可以通过以下方式获得排毒以添加您自己的参数:
detox.init(config, { launchApp: false });
device.launchApp({ newInstance: true, launchArgs: {
myCustomArg: value,
...,
} });这将是很好的抛光到一个模块在某个时间点。
https://stackoverflow.com/questions/47686303
复制相似问题