0

We have an Azure DevOps pipeline which uses self hosted Windows agents with Azure DevOps server 2019. The pipeline runs our front-end tests. For this we use the following command line to run the tests: npm run jest -- --ci --reporters=default --reporters=jest-junit. Then we use the publish test results task to publish the results.

This all works just fine. However, we noticed recently that the runtime warnings in the tests aren't being displayed anywhere. We have our linter warnings displayed in the build results page by adding the vso formatter like this: npm run nx run-many -- --target="lint" --all --skip-nx-cache=true --parallel --format=vso. However, it doesn't seem jest has any kind of format argument we can use.

Is it possible to take the warnings that display in the jest tests and log them in the results page of the build? Thank you for any help, please let me know if I can provide additional information.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Andrew
  • 585
  • 1
  • 7
  • 17
  • 1
    How about using [logging commands](https://github.com/microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md)? –  Dec 18 '20 at 10:04
  • @HiGuy I've looked into that, and it would log the warnings like I want. I understand you can use them to manually create logs but I don't see how I could get the warnings from jest and log them using only the logging commands. – Andrew Dec 18 '20 at 14:08
  • 1
    @Andrew, you can try to use `console.error` or `console.warn` on the jest test file. You can check this [github link](https://github.com/facebook/jest/issues/6121#issuecomment-708330601). – Mr Qian Dec 24 '20 at 06:49
  • @PerryQian-MSFT Thank you for that link. I needed to adjust it slightly but it got me on the right track to get a decent solution. – Andrew Dec 24 '20 at 17:47

1 Answers1

0

So I ended up using the following PowerShell task to append a version of what @PerryQian-MSFT posted into my jest-setup.js file.

- 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));
        };
      "@

I had to change the solution from the GitHub post because I didn't want the tests to fail if they hit a warning, the pipeline should still succeed, just with issues. To fix this I included azure-pipelines-logging as a dependency. Then I was able to use log(command("task", "logissue", { type: "warning" })(...args)); to log in the pipeline whenever a warning is called.

Andrew
  • 585
  • 1
  • 7
  • 17