" ReferenceError:未定义设备"在React Native for android中使用detox + Jest时

时间:2018-01-08 10:07:57

标签: react-native detox

App在Android和iOS模拟器中都运行良好。我正在尝试使用Jest设置Detox,以便对我的react-native应用程序进行端到端测试。在模拟器中,我可以看到一些动作,但app没有启动,我得到以下错误(除了"设备未定义"我也得到" 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();
});

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

1 个答案:

答案 0 :(得分:0)

我能够通过跟踪排除this example来解决问题。跟随android项目配置并在app/build.gradle中进行了一些更改。基本上与示例代码中的相同。

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