使用 NX 进行剧作家测试

Eug*_*van 2 node.js playwright nx-workspace

我有一个 NX 工作区,其中有一个名为 的应用程序my-app。我想my-app使用 NX 控制台运行应用程序的 Playwright 测试。目前 NX 不支持 Playwright 插件,因此我根据本教程创建了一个自定义 NX 执行器。我已经为执行者创建了必要的文件。之后,我在应用程序的project.json文件中注册了自定义 e2e 命令。剧作家配置文件保留在该my-app文件夹中。

当我运行时nx run my-app:e2e,执行程序已被执行,但由于某种原因,剧作家没有启动。相反,我看到一个错误。

在此输入图像描述

当我在控制台中手动运行时,剧作家触发的命令启动并进行必要的nx run my-app:e2e测试。npx playwright test --config=apps/my-app/playwright.config.ts

项目.json

...
...
...
"e2e": {
  "executor": "./tools/executors/playwright:playwright",
  "options": {
    "path": "apps/my-app/playwright.config.ts"
  }
}
Run Code Online (Sandbox Code Playgroud)

执行器.json

{
  "executors": {
    "playwright": {
      "implementation": "./impl",
      "schema": "./schema.json",
      "description": "Runs Playwright Test "
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

隐含的ts

export default async function echoExecutor(
  options: PlaywrightExecutorOptions,
  context: ExecutorContext
) {
  console.info(`Executing "Playwright"...`);
  console.info(`Options: ${JSON.stringify(options, null, 2)}`);

  const { stdout, stderr } = await promisify(exec)(
    `npx playwright test --config=${options.path}`,
  );
  console.log(stdout);
  console.error(stderr);

  const success = !stderr;
  return { success };
}
Run Code Online (Sandbox Code Playgroud)

架构.json

{
  "$schema": "http://json-schema.org/schema",
  "type": "object",
  "cli": "nx",
  "properties": {
    "path": {
      "type": "string",
      "description": "Path to the project"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "executors": "./executor.json"
}
Run Code Online (Sandbox Code Playgroud)

我不确定,但也许问题出在promisify?我正在尝试用npx它打电话。也许在这种情况下有不同的调用方式npx

  const { stdout, stderr } = await promisify(exec)(
    `npx playwright test --config=${options.path}`,
  );
Run Code Online (Sandbox Code Playgroud)

Abh*_*pta 5

我会推荐https://github.com/marksandspencer/nx-plugins/tree/main/packages/nx-playwright

您需要使用安装插件

yarn add --dev @mands/nx-playwright
yarn playwright install --with-deps
Run Code Online (Sandbox Code Playgroud)

nx generate remove <APP-NAME>-e2e在生成新的 e2e 应用程序之前删除现有的 e2e 应用程序

生成新的 e2e 应用程序

yarn nx generate @mands/nx-playwright:project <APP-NAME>-e2e --project <APP-NAME>

PS:我也是该插件的作者