Getting ReferenceError: device is not defined when running detox test on react-native ios

Getting ReferenceError: device is not defined when running detox test on react-native ios

我正在尝试将排毒测试添加到工作中的 react-native 应用程序,但是当 运行 detox test 时出现错误 这是错误消息:

detox test
detox[45074] INFO:  [test.js] configuration="ios.sim.debug" reportSpecs=true DETOX_START_TIMESTAMP=1573653557623 node_modules/.bin/jest --config e2e/config.json '--testNamePattern=^((?!:android:).)*$' --maxWorkers 1 "e2e"
● Validation Warning:

  Unknown option "setupFilesAfterEnv" with value ["./init.js"] was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration.html

 FAIL  e2e/firstTest.spec.jsun...
  loading search screen and using search inputText
    ✕ search screen should be visible (1ms)
    ✕ should show search filter Menu after tap (1ms)
    ✕ should show search list after writing in input field

  ● loading search screen and using search inputText › search screen should
 be visible

    ReferenceError: device is not defined

      1 | describe('loading search screen and using search inputText', () => {
      2 |   beforeAll(async () => {
    > 3 |     await device.reloadReactNative();
        |             ^
      4 |   });
      5 | 
      6 |   it('search screen should be visible', async () => {

      at Object._callee$ (firstTest.spec.js:3:13)
      at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:288:22)
      at Generator.prototype.<computed> [as next] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:114:21)
      at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at invoke (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:152:20)
      at ../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:187:11
      at callInvokeWithMethodAndArg (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:186:16)
      at AsyncIterator.enqueue (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:209:13)

  ● loading search screen and using search inputText › search screen should be visible

    ReferenceError: element is not defined

      4 |   });
      5 | 
    > 6 |   it('search screen should be visible', async () => {
        |                                                                                    ^
      7 |     await expect(element(by.id('searchPageContainer'))).toBeVisible();
      8 |   });
      9 | 

      at Object._callee2$ (firstTest.spec.js:6:267)
      at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:288:22)
      at Generator.prototype.<computed> [as next] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:114:21)
      at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at invoke (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:152:20)
      at ../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:187:11
      at callInvokeWithMethodAndArg (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:186:16)
      at AsyncIterator.enqueue (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:209:13)

  ● loading search screen and using search inputText › should show search filter Menu after tap

    ReferenceError: device is not defined

      1 | describe('loading search screen and using search inputText', () => {
      2 |   beforeAll(async () => {
    > 3 |     await device.reloadReactNative();
        |             ^
      4 |   });
      5 | 
      6 |   it('search screen should be visible', async () => {

      at Object._callee$ (firstTest.spec.js:3:13)
      at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:288:22)
      at Generator.prototype.<computed> [as next] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:114:21)
      at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at invoke (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:152:20)
      at ../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:187:11
      at callInvokeWithMethodAndArg (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:186:16)
      at AsyncIterator.enqueue (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:209:13)

  ● loading search screen and using search inputText › should show search filter Menu after tap

    ReferenceError: element is not defined

       8 |   });
       9 | 
    > 10 |   it('should show search filter Menu after tap', async () => {
         |                                                                                             ^
      11 |     await element(by.id('filterMenuButton')).tap();
      12 |     await expect(element(by.id('allMosqueMenuItem'))).toBeVisible();
      13 |   });

      at Object._callee3$ (firstTest.spec.js:10:276)
      at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:288:22)
      at Generator.prototype.<computed> [as next] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:114:21)
      at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at invoke (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:152:20)
      at ../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:187:11
      at callInvokeWithMethodAndArg (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:186:16)
      at AsyncIterator.enqueue (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:209:13)

  ● loading search screen and using search inputText › should show search list after writing in input field

    ReferenceError: device is not defined

      1 | describe('loading search screen and using search inputText', () => {
      2 |   beforeAll(async () => {
    > 3 |     await device.reloadReactNative();
        |             ^
      4 |   });
      5 | 
      6 |   it('search screen should be visible', async () => {

      at Object._callee$ (firstTest.spec.js:3:13)
      at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:288:22)
      at Generator.prototype.<computed> [as next] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:114:21)
      at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at invoke (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:152:20)
      at ../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:187:11
      at callInvokeWithMethodAndArg (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:186:16)
      at AsyncIterator.enqueue (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:209:13)

  ● loading search screen and using search inputText › should show search list after writing in input field

    ReferenceError: element is not defined

      13 |   });
      14 | 
    > 15 |   it('should show search list after writing in input field', async () => {
         |                                                                                                         ^
      16 |     await element(by.id('searchTextInput')).typeText('paris fr');
      17 |     await expect(element(by.id('searchList'))).toBeVisible();
      18 |   });

      at Object._callee4$ (firstTest.spec.js:15:288)
      at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:288:22)
      at Generator.prototype.<computed> [as next] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:114:21)
      at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at invoke (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:152:20)
      at ../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:187:11
      at callInvokeWithMethodAndArg (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:186:16)
      at AsyncIterator.enqueue (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:209:13)

detox[45074] ERROR: [cli.js] Error: Command failed: node_modules/.bin/jest 
--config e2e/config.json '--testNamePattern=^((?!:android:).)*$' --maxWorke
rs 1 "e2e"

这是我在 package.json 中的排毒配置:

"detox": {
    "configurations": {
      "ios.sim.debug": {
        "binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/MdcIos.app",
        "build": "xcodebuild -workspace ios/MdcIos.xcworkspace -scheme MdcIos -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build",
        "type": "ios.simulator",
        "device": {
          "type": "iPhone 11"
        }
      }
    },
    "test-runner": "jest"
  }

还有 e2e 文件夹中的 config.json:

{
    "setupFilesAfterEnv": ["./init.js"],
    "testEnvironment": "node",
    "reporters": ["detox/runners/jest/streamlineReporter"],
    "verbose": true,
    "transform": {
        "^.+\.js$": "<rootDir>/../node_modules/react-native/jest/preprocessor.js"
    }
}

最后 init.js 在 e2e 中:

const detox = require('detox');
const config = require('../package.json').detox;
const adapter = require('detox/runners/jest/adapter');
const specReporter = require('detox/runners/jest/specReporter');

// Set the default timeout
jest.setTimeout(120000);
jasmine.getEnv().addReporter(adapter);

// This takes care of generating status logs on a per-spec basis. By default, jest only reports at file-level.
// This is strictly optional.
jasmine.getEnv().addReporter(specReporter);

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

beforeEach(async () => {
  await adapter.beforeEach();
});

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

谢谢!

我发现了问题; package.json 中的排毒配置中的 binaryPath 不好。

"binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/APP_NAME_WAS_FALSE.app"

运行 adb devices命令并检查设备id并将id赋予package.json

中的名称
"detox": {
    "configurations": {
      "ios.sim.debug": {
        "binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/MdcIos.app",
        "build": "xcodebuild -workspace ios/MdcIos.xcworkspace -scheme MdcIos -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build",
        "type": "ios.simulator",
        "name" : 520084b3b6ef7473(device id),
        "device": {
          "type": "iPhone 11"
        }
      }
    },
    "test-runner": "jest"
  }