我在17.13.2
版本中运行detox
,并使用jest-circus
作为测试运行器。我的主要问题是,在我运行测试之后或之前,应用程序没有重置,这导致应用程序的状态不一致。
我的测试文件:
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()
的一些默认值
{
"detox": {
"behavior": {
"init": {
"reinstallApp": true,
"launchApp": true,
"exposeGlobals": true
},
"cleanup": {
"shutdownDevice": false
}
}
}
}
然而,这似乎不足以在运行测试后实际擦除应用程序状态。
我尝试以setupFilesAfterEnv
的身份使用初始化脚本,该脚本将在测试套件运行后调用cleanup()
。实际上,这适用于仍然使用jasmine 2
作为测试运行器的旧项目。
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
,但它不是,尽管我在配置文件中指定了:
{
"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
,但我不明白如何才能访问排毒生命周期挂钩。
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中应该把我的可重用生命周期钩子放在哪里。此外,我想知道是否需要这些自定义配置来重新安装应用程序并在每个测试套件之前擦除应用程序数据。
发布于 2020-12-29 16:00:18
您仍然可以使用setupFilesAfterEnv
,但您不应该像以前那样调用相同的钩子/清理(请参阅https://github.com/wix/Detox/issues/2410#issuecomment-744387707)。
以下是我们的init脚本的示例:
// 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([
// ...
]);
});
https://stackoverflow.com/questions/65126608
复制