【问题标题】:detox ReferenceError: before is not defineddetox ReferenceError: before is not defined
【发布时间】:2018-12-26 08:02:39
【问题描述】:

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

我只安装了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"
      }

}

【问题讨论】:

    标签: javascript react-native e2e-testing detox


    【解决方案1】:

    我看起来你正在尝试在 jest runner 上运行 mocha 测试。由于您的 init.js 是为 mocha 设置的,但您使用的测试运行器是开玩笑的。您收到的错误消息node_modules/.bin/jest e2e... 证实了这一点。

    你应该选择一个,玩笑或摩卡咖啡并使用它。而不是试图同时使用两者。

    #开玩笑 如果您使用 jest,您的 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 中的 detox 对象中。

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

    {
      "setupFilesAfterEnv" : ["./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 中的 detox 对象中删除 "test-runner": "jest",因为它不是必需的。

    您的init.js 旁边应该有一个mocha.opts 文件,而不是config.json 文件,它应该有类似的内容:

    --recursive
    --timeout 120000
    --bail
    

    #接下来的步骤

    1. 选择您要运行的测试运行器; 开玩笑摩卡
    2. 确保您拥有适用于测试运行器的正确 init.js 文件。
    3. 如果使用 jest 有一个 config.json 文件并将 test-runner 添加到 package.json 中的 detox 对象。
    4. 如果使用 mocha 有一个 mocha.opts 文件。无需在 package.json 的 detox 对象中指定 test-runner。

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

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

    【讨论】:

    • 我不知道这是一个选项。谢谢你。 :)
    猜你喜欢
    • 1970-01-01
    • 2014-03-03
    • 2020-06-14
    • 2019-02-08
    • 2015-11-19
    • 2019-11-10
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多