【问题标题】:Display jest warning as a warning in the Azure DevOps pipeline build results page在 Azure DevOps 管道生成结果页面中将开玩笑警告显示为警告
【发布时间】:2020-12-25 01:46:19
【问题描述】:

我们有一个 Azure DevOps 管道,它使用 Azure DevOps server 2019 的自托管 Windows 代理。该管道运行我们的前端测试。为此,我们使用以下命令行来运行测试: npm run jest -- --ci --reporters=default --reporters=jest-junit。然后我们使用发布测试结果任务来发布结果。

这一切都很好。但是,我们最近注意到测试中的运行时警告没有显示在任何地方。通过像这样添加 vso 格式化程序,我们在构建结果页面中显示了 linter 警告:npm run nx run-many -- --target="lint" --all --skip-nx-cache=true --parallel --format=vso。但是,jest 似乎没有任何我们可以使用的格式参数。

是否可以获取在开玩笑测试中显示的警告并将它们记录在构建的结果页面中?感谢您的帮助,如果我可以提供更多信息,请告诉我。

【问题讨论】:

  • logging commands怎么样?
  • @HiGuy 我已经调查过了,它会像我想要的那样记录警告。我知道您可以使用它们手动创建日志,但我不知道如何从 jest 中获取警告并仅使用日志记录命令记录它们。
  • @Andrew,您可以尝试在开玩笑的测试文件上使用console.errorconsole.warn。你可以查看这个github link
  • @PerryQian-MSFT 谢谢你的链接。我需要稍微调整一下,但它让我走上了正确的轨道,得到了一个不错的解决方案。

标签: azure-devops jestjs azure-devops-pipelines jest-junit


【解决方案1】:

所以我最终使用以下 PowerShell 任务将 @PerryQian-MSFT 发布的内容附加到我的 jest-setup.js 文件中。

- task: PowerShell@2
  displayName: Make test log warnings
  inputs:
    targetType: 'inline'
    script: |
      Add-Content -path config/jest-setup.js -value @"
        import { command, log } from "azure-pipelines-logging";
        const { error, warn } = console;

        global.console.error = (...args) => {
          error(...args);
          log(command("task", "logissue", { type: "error" })(...args));
        };
        global.console.warn = (...args) => {
          warn(...args);
          log(command("task", "logissue", { type: "warning" })(...args));
        };
      "@

我不得不更改 GitHub 帖子中的解决方案,因为我不希望测试在遇到警告时失败,管道应该仍然成功,只是有问题。为了解决这个问题,我将azure-pipelines-logging 包含为依赖项。然后我可以使用log(command("task", "logissue", { type: "warning" })(...args)); 登录管道,只要调用警告。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 2018-09-18
    相关资源
    最近更新 更多