gpt4 book ai didi

react-native - "ReferenceError: device is not defined"在 React Native for android 中使用 detox+Jest 时

转载 作者:行者123 更新时间:2023-12-03 17:35:58 27 4
gpt4 key购买 nike

应用程序在 Android 和 iOS 模拟器中都运行良好。我正在尝试使用 Jest 设置 Detox 以进行端到端测试我的 react-native 应用程序。在模拟器中,我可以看到一些移动,但应用程序未启动,并且出现以下错误(除了“设备未定义”之外,我还收到“app-debug-androidTest.apk:没有这样的文件或目录”错误,所以不确定是什么是这里的根本原因)。

控制台

>detox test --configuration android.emu.debug
node_modules/.bin/jest e2e --config=e2e/config.json --runInBand
server listening on localhost:64281...
(node:75639) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
console.log e2e/firstTest.spec.js:3
**********before each called

console.log e2e/firstTest.spec.js:9
***************first test

FAIL e2e/firstTest.spec.js (123.358s)
Example
✕ should have welcome screen (120009ms)

● Example › should have welcome screen

ChildProcessError: Command failed: /Users/xxx/Library/Android/sdk/platform-tools/adb -s emulator-5554 install -r -g /Users/xxx/Projects/xxxxxx/android/app/build/outputs/apk/app-debug-androidTest.apk
adb: failed to stat /Users/xxx/Projects/xxxxxx/android/app/build/outputs/apk/app-debug-androidTest.apk: No such file or directory
`/Users/xxx/Library/Android/sdk/platform-tools/adb -s emulator-5554 install -r -g /Users/xxx/Projects/xxxxxx/android/app/build/outputs/apk/app-debug-androidTest.apk` (exited with error code 1)

at callback (../node_modules/child-process-promise/lib/index.js:33:27)

● Example › should have welcome screen

ReferenceError: device is not defined

2 | beforeEach(async () => {
3 | console.log('**********before each called');
> 4 | await device.reloadReactNative();
5 | console.log('**********before each end');
6 | });
7 |

at Object._callee$ (firstTest.spec.js:4:1)
at tryCatch (../node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (../node_modules/regenerator-runtime/runtime.js:296:22)
at Generator.prototype.(anonymous function) [as next] (../node_modules/regenerator-runtime/runtime.js:114:21)
at tryCatch (../node_modules/regenerator-runtime/runtime.js:62:40)
at invoke (../node_modules/regenerator-runtime/runtime.js:152:20)
at ../node_modules/regenerator-runtime/runtime.js:195:11
at callInvokeWithMethodAndArg (../node_modules/regenerator-runtime/runtime.js:194:16)
at AsyncIterator.enqueue (../node_modules/regenerator-runtime/runtime.js:217:13)
at AsyncIterator.prototype.(anonymous function) [as next] (../node_modules/regenerator-runtime/runtime.js:114:21)
at Object.<anonymous>.runtime.async (../node_modules/regenerator-runtime/runtime.js:241:14)
at Object._callee (firstTest.spec.js:2:57)

● Example › should have welcome screen

Timeout - Async callback was not invoked within the 120000ms timeout specified by jest.setTimeout.

at ../node_modules/jest-jasmine2/build/queue_runner.js:72:21
at Timeout.callback [as _onTimeout] (../node_modules/jsdom/lib/jsdom/browser/Window.js:592:19)

Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 123.919s, estimated 241s
Ran all test suites matching /e2e/i.
child_process.js:644
throw err;
^

Error: Command failed: node_modules/.bin/jest e2e --config=e2e/config.json --runInBand
at checkExecSyncError (child_process.js:601:13)
at Object.execSync (child_process.js:641:13)
at runJest (/Users/xxx/Projects/xxxxxx/node_modules/detox/local-cli/detox-test.js:69:6)
at Object.<anonymous> (/Users/xxx/Projects/xxxxxx/node_modules/detox/local-cli/detox-test.js:42:5)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)

firstTest.spec.js:
describe('Example', () => {
beforeEach(async () => {
console.log('**********before each called');
await device.reloadReactNative();
console.log('**********before each end');
});

it('should have welcome screen', async () => {
console.log('***************first test');
await expect(element(by.id('welcome'))).toBeVisible();
console.log('***************first test end');
});
})

package.json
"dependencies": {
"react": "16.0.0",
"react-native": "0.51.0",
....
},
"devDependencies": {
"babel-jest": "22.0.3",
"babel-preset-react-native": "4.0.0",
"detox": "^6.0.4",
"jest": "22.0.3",
"react-test-renderer": "16.0.0"
},
"jest": {
"preset": "react-native",
"setupTestFrameworkScriptFile": "./e2e/init.js"
},
"detox": {
"specs": "e2e",
"test-runner": "jest",
"runner-config": "e2e/config.json",
"configurations": {
"android.emu.debug": {
"binaryPath": "android/app/build/outputs/apk/app-debug.apk",
"build": "pushd android && ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug && popd",
"type": "android.emulator",
"name": "Pixel_API_27"
},
"android.emu.release": {
"binaryPath": "android/app/build/outputs/apk/release/app-release.apk",
"build": "cd android && ./gradlew assembleRelease assembleAndroidTest -DtestBuildType=release && cd ..",
"type": "android.emulator",
"name": "Pixel_API_27"
}
}
}

init.js
const detox = require('detox');
const config = require('../package.json').detox;

// Set the default timeout
jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000;

beforeAll(async () => {
await detox.init(config);
});

afterAll(async () => {
await detox.cleanup();
});

以下是运行测试命令时的 View
Emulator while running test

最佳答案

我可以通过关注 this example 来解决这个问题从排毒。遵循android项目配置并在app/build.gradle中做了一些更改.基本上与示例代码中的相同。

等待 detox 以明确的设置步骤正式开始支持 Android。使用 gradle 3 时仍然存在问题。

关于react-native - "ReferenceError: device is not defined"在 React Native for android 中使用 detox+Jest 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48148064/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com