title: "ReferenceError: device is not defined" when using detox+Jest in React Native for android
body: <p>App is working fine both in Android and iOS simulators. I am trying to setup Detox with Jest for end to end testing my react-native app. In simulator I can see some movement but app is not launched and I get following errors(apart from "device not defined" I am also getting "app-debug-androidTest.apk: No such file or directory" error, so not sure what is the root cause here).</p>
  
  <p><strong>Console</strong></p>
  
  <pre><code>&gt;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 () =&gt; {
        3 |     console.log('**********before each called');
      &gt; 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.&lt;anonymous&gt;.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.&lt;anonymous&gt; (/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)
  </code></pre>
  
  <p>firstTest.spec.js:</p>
  
  <pre><code>describe('Example', () =&gt; {
    beforeEach(async () =&gt; {
      console.log('**********before each called');
      await device.reloadReactNative();
      console.log('**********before each end');
    });
  
    it('should have welcome screen', async () =&gt; {
      console.log('***************first test');
      await expect(element(by.id('welcome'))).toBeVisible();
      console.log('***************first test end');
    });
  })
  </code></pre>
  
  <p><strong>package.json</strong></p>
  
  <pre><code>"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 &amp;&amp; ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug &amp;&amp; popd",
          "type": "android.emulator",
          "name": "Pixel_API_27"
        },
        "android.emu.release": {
          "binaryPath": "android/app/build/outputs/apk/release/app-release.apk",
          "build": "cd android &amp;&amp; ./gradlew assembleRelease assembleAndroidTest -DtestBuildType=release &amp;&amp; cd ..",
          "type": "android.emulator",
          "name": "Pixel_API_27"
        }
      }
    }
  </code></pre>
  
  <p><strong>init.js</strong></p>
  
  <pre><code>const detox = require('detox');
  const config = require('../package.json').detox;
  
  // Set the default timeout
  jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000;
  
  beforeAll(async () =&gt; {
    await detox.init(config);
  });
  
  afterAll(async () =&gt; {
    await detox.cleanup();
  });
  </code></pre>
  
  <p>Following is the view while running the test command
  <a href="https://i.stack.imgur.com/Omo1t.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/Omo1t.jpg" alt="Emulator while running test"></a></p>