Search code examples
reactjsunit-testingjestjsazure-pipelines

Jest unit Test cases are not executing from Azure DevOps Pipeline


In the React project, It has front-end unit test cases. From the VS Code terminal, when I run those using the npm run test

>npm run test

> [email protected] test
> react-scripts test --env=jsdom

The test cases are running and producing results. Here is the part of the package.json file

 "devDependencies": {
        "@babel/core": "^7.21.0",
        "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
        "@types/applicationinsights-js": "^1.0.9",
        "@types/history": "^4.7.8",
        "@types/node": "^15.0.3",
        "@types/react": "^17.0.5",
        "@types/react-dom": "^17.0.4",
        "@types/react-select": "^5.0.1",
        "eslint-plugin-biis": "^1.0.2",
        "fetch-mock": "^9.11.0",
        "postcss": "^8.4.21",
        "postcss-flexbugs-fixes": "^5.0.2",
        "postcss-loader": "^7.0.2",
        "postcss-normalize": "^10.0.1",
        "postcss-preset-env": "^8.0.1",
        "prettier": "^2.8.8",
        "prettier-plugin-jsdoc": "^0.3.38",
        "prettier-plugin-organize-imports": "^2.3.3",
        "react-scripts": "^5.0.1",
        "typescript": "^4.9.3"
    },
    "scripts": {
        "start": "react-scripts --openssl-legacy-provider start",
        "build": "react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject",
        "eslint": "eslint src/**/*.js",
        "legacy-install": "npm install --legacy-peer-deps",
        "clean-start": "rm -rf node_modules && npm run legacy-install && npm run start"        
    },

Now, I am trying to execute the unit test cases from the Azure DevOps pipeline. To do so, in the build pipeline, the npm command has been added to run the command npm run test. The package.json file is the root directory which is defined by $(WorkingDirectory). Here is the setting: enter image description here

When the pipeline runs and reaches the task "run unit test", it doesn't execute the test cases. In the debug log, it doesn't show any error but the pipeline timeout after running for 60 minutes. Here is the output from the pipeline log:

##[debug]arguments:
##[debug]   run
##[debug]   test
C:\Windows\system32\cmd.exe /D /S /C "C:\hostedtoolcache\windows\node\16.3.0\x64\npm.cmd run test"

What is missing here?

I googled the issue to find the solution, but no luck.


Solution

  • I can reproduce the same issue when using the NPM Task version 1. It will stuck at the npm run test command without any output.

    enter image description here

    It seems that the issue is from the NPM task version 1 itself. I suggest that you can report the issue in Developer Community

    Azure DevOps developers will further investigate and resolve issues with Azure DevOps Pipeline task itself.

    You can refer to the following methods to workaround this issue:

    Method1: You can change to use the NPM Task version 0 and set the npm run test command.

    For example:

    enter image description here

    Method2: You can change to use CommandLine/PowerShell/Bash task to run the npm run test command:

    For example:

    enter image description here

    In this case, the npm run test command can work as expected.

    Result:

    enter image description here