首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在最新版本中配置排毒生命周期钩子?

如何在最新版本中配置排毒生命周期钩子?
EN

Stack Overflow用户
提问于 2020-12-03 13:08:19
回答 1查看 342关注 0票数 0

我在17.13.2版本中运行detox,并使用jest-circus作为测试运行器。我的主要问题是,在我运行测试之后或之前,应用程序没有重置,这导致应用程序的状态不一致。

我的测试文件:

代码语言:javascript
运行
AI代码解释
复制
import { by, device, element, expect, waitFor } from "detox"

describe("Login", () => {
  beforeEach(async () => {
    await device.reloadReactNative()
  })

  it("should login with correct data", async () => {
    await element(by.id("login_email_input")).typeText("ch.tietz@gmail.com")
    await element(by.id("login_password_input")).typeText("12345678")
    await element(by.id("login_submit_button")).tap()

    await waitFor(element(by.id("workout_screen"))).toBeVisible()

    // additional test steps

  })
})

现在,如果登录确实有效,但在后续步骤中测试失败,则应用程序用户的状态仍将是“已登录”。

据我所知,实际上改变应用程序状态是不可能的,例如通过清除AsyncStorage或直接与状态管理工具交互。相反,建议只重新安装应用程序-但这正是我正在苦苦挣扎的地方。

我尝试了许多方法,但都没有奏效。真正让人难以理解configuration is that detox completely changed how the configuration works的是什么,并切换到jest-circus作为主要的测试跑步者。

我的设置基本上是由jest init -r jest创建的。据我所知,这已经包含了detox.init()detox.cleanup()的一些默认值

代码语言:javascript
运行
AI代码解释
复制
{
  "detox": {
    "behavior": {
      "init": {
        "reinstallApp": true,
        "launchApp": true,
        "exposeGlobals": true
      },
      "cleanup": {
        "shutdownDevice": false
      }
    }
  }
}

然而,这似乎不足以在运行测试后实际擦除应用程序状态。

我尝试以setupFilesAfterEnv的身份使用初始化脚本,该脚本将在测试套件运行后调用cleanup()。实际上,这适用于仍然使用jasmine 2作为测试运行器的旧项目。

代码语言:javascript
运行
AI代码解释
复制
import { cleanup, init } from 'detox';
const adapter = require('detox/runners/jest/adapter');
const specReporter = require('detox/runners/jest/specReporter');

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

// 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 init(config, { launchApp: false });
}, 300000);

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

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

首先,它抱怨没有定义jasmine。我猜这是因为实际上本例中的adapter应该是一个DetoxAdapterCircus,但它不是,尽管我在配置文件中指定了:

代码语言:javascript
运行
AI代码解释
复制
{
    "preset": "react-native",
    "testEnvironment": "./environment.ts",
    "testRunner": "jest-circus/runner",
    "testTimeout": 120000,
    "testRegex": "\\.e2e\\.ts$",
    "reporters": ["detox/runners/jest/streamlineReporter"],
    "verbose": true
}

"testRunner": "jest-circus/runner"

另一个想法是修改CustomDetoxEnvironment,但我不明白如何才能访问排毒生命周期挂钩。

代码语言:javascript
运行
AI代码解释
复制
const {
  DetoxCircusEnvironment,
  SpecReporter,
  WorkerAssignReporter,
} = require("detox/runners/jest-circus")

class CustomDetoxEnvironment extends DetoxCircusEnvironment {
  constructor(config) {
    super(config)

    // should I access the hooks here now?

    // This takes care of generating status logs on a per-spec basis. By default, Jest only reports at file-level.
    // This is strictly optional.
    this.registerListeners({
      SpecReporter,
      WorkerAssignReporter,
    })
  }
}

module.exports = CustomDetoxEnvironment

我不知道在最新版本的Detox中应该把我的可重用生命周期钩子放在哪里。此外,我想知道是否需要这些自定义配置来重新安装应用程序并在每个测试套件之前擦除应用程序数据。

EN

回答 1

Stack Overflow用户

发布于 2020-12-29 16:00:18

您仍然可以使用setupFilesAfterEnv,但您不应该像以前那样调用相同的钩子/清理(请参阅https://github.com/wix/Detox/issues/2410#issuecomment-744387707)。

以下是我们的init脚本的示例:

代码语言:javascript
运行
AI代码解释
复制
// init.ts
// with "setupFilesAfterEnv": ["./init.ts"] in the conf

beforeAll(async () => {
  // to reset the state
  await device.clearKeychain();

  // we are launching the app manually
  await device.launchApp({
    permissions: { notifications: 'YES', location: 'inuse' },
  });

  await device.setURLBlacklist([
    // ...
  ]);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65126608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档