如何使用 Detox 和 Jest 为 React Native 应用程序按特定顺序运行许多 e2e 文件?

问题描述 投票:0回答:2

如前所述,我正在使用 Detox(和 Jest)测试 React Native 应用程序,我希望有几个具有不同目的的 e2e 文件 - 例如:登录、填写表单等 - 并按特定顺序运行它们(应该先登录 e2e 文件)。以随机顺序运行它们是行不通的。

目标是避免拥有一个巨大的文件。

注意:我正在 iOS 模拟器上运行测试。

ios react-native testing jestjs detox
2个回答
0
投票

简短的回答:你不能,但请继续阅读。

Jest的概念模型是每个测试文件都是一个单元,并且与其他文件完全隔离。它使事情更容易推理并且允许并行化。如果您的测试需要按特定顺序运行,那么它们在逻辑上是一个单元,因此需要在单个测试文件中指定。

但是,这并不妨碍您将测试拆分为多个文件。您可以拥有一个 Jest 可以识别的文件(例如

full-suite.e2e.js
),并让该文件包含其他几个文件(例如
login.js
forms.js
等)。这样,Jest 将所有内容作为一个文件按照您指定的顺序运行,但您可以以一种对您来说具有逻辑意义的方式组织您的各个测试。


0
投票

每个人都告诉你由于某种原因不能这样做,而这是 Jest 中的默认设置,这很荒谬。

const fs = require("fs");
const { execSync } = require("child_process");
const path = require("path");

const testFolder = "./e2e";

// Read and sort test files
const testFiles = fs
  .readdirSync(testFolder)
  .filter((file) => file.endsWith(".js")) // Ensure only JavaScript files are considered
  .sort(); // Sorts files alphabetically, assuming they're prefixed as 01_, 02_, etc.

// Execute each test file sequentially
for (const file of testFiles) {
  console.log(`Running test file: ${file}`);
  const filePath = path.join(testFolder, file);
  try {
    execSync(`npx detox test ${filePath} --configuration ios.release`, { stdio: "inherit" });
  } catch (error) {
    console.error(`Test failed: ${file}`, error);
    break; // Stops running further tests if one fails
  }
}

有一个节点脚本。

将其另存为

runInOrder.js
并像
node runInOrder
一样使用它,它将按名称按顺序运行您的测试。

© www.soinside.com 2019 - 2024. All rights reserved.