排毒ReferenceError:之前未定义

时间:2018-07-18 11:53:50

标签: javascript react-native e2e-testing detox

我正在使用排毒测试工具,但遇到了困难。

我只安装了Detox,我只运行了ios测试的基本代码,但出现以下错误:

请帮助我。

仅iOS

错误日志

$ detox test --configuration ios.sim.debug --debug-synchronization --take-screenshots all --record-videos nonex --record-logs all
node_modules/.bin/jest e2e --config=e2e/config.json --maxWorkers=1 --testNamePattern='^((?!:android:).)*$'
 FAIL  e2e/firstTest.spec.js
  ● Test suite failed to run

    ReferenceError: before is not defined

      3 | const adapter = require('detox/runners/mocha/adapter');
      4 | 
    > 5 | before(async () => {
        | ^
      6 |   await detox.init(config);
      7 | });
      8 | 

      at Object.<anonymous> (init.js:5:1)

package.json

"script":{
   "e2e:ios": "detox test --configuration ios.sim.debug --debug-synchronization --take-screenshots all --record-videos nonex --record-logs all",
   "e2e:android": "detox test --configuration android.emu.debug --loglevel verbose --take-screenshots all --record-videos none --record-logs all"
        },
   dependencies": {
        "detox": "^8.0.0",
        "jest": "^23.1.0",
        "mocha": "^5.2.0",
    },
    "detox": {
        "configurations": {
          "ios.sim.debug": {
            "binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/{app_name[enter image description here][1]}.app",
            "build": "xcodebuild -workspace ios/{workspace_Name}.xcworkspace -scheme {scheme_name} Dev -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build",
            "type": "ios.simulator",
            "name": "iPhone 7"
          },
          "android.emu.debug": {
            "binaryPath": "android/app/build/outputs/apk/dev/debug/{apk_name}.apk",
            "build": "react-native run-android --variant=devDebug --appId com.noahclient.dev",
            "type": "android.emulator",
            "name": "Nexus_5X_API_26"
          }
        },
        "test-runner": "jest"
      }

}

1 个答案:

答案 0 :(得分:2)

我似乎您正在尝试对开玩笑的跑步者进行摩卡测试。由于您的init.js是为摩卡咖啡设置的,但是您使用的测试运行程序只是开玩笑。您收到的错误消息node_modules/.bin/jest e2e...确认了这一点。

您应该选择一个,开玩笑的或摩卡咖啡,然后使用它。而不是尝试同时使用两者。

开玩笑

如果您使用的是笑话,则init.js应该如下所示:

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

jest.setTimeout(120000);
jasmine.getEnv().addReporter(adapter);

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

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

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

,您应该将"test-runner": "jest"添加到package.json中的排毒对象中。

您还应该在与config.json相同的位置包含一个init.js文件,

{
  "setupTestFrameworkScriptFile" : "./init.js"
}

摩卡咖啡

如果您正在使用 mocha ,则您的init.js应如下所示:

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

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

beforeEach(async function () {
  await adapter.beforeEach(this);
});

afterEach(async function () {
  await adapter.afterEach(this);
});

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

,您应该从package.json中的排毒对象中删除"test-runner": "jest",因为这不是必需的。

config.json旁边应该有一个mocha.opts文件,而不是init.js文件,并且文件应该类似于:

--recursive
--timeout 120000
--bail

下一步

  1. 选择要运行的测试运行程序; 开玩笑摩卡
  2. 确保您具有测试运行程序的正确init.js文件。
  3. 如果使用 jest ,请拥有config.json文件,并将测试运行程序添加到package.json中的排毒对象。
  4. 如果使用 mocha ,则具有mocha.opts文件。无需在package.json的排毒对象中指定测试运行器。

您可以在此处查看设置说明:https://github.com/wix/detox/blob/master/docs/Introduction.GettingStarted.md#step-3-create-your-first-test

如果您仍有问题,请告诉我。