2 | 2021

Run Unit tests in Azure Pipelines with Jest

Hardware has become a topic of interest for a wide variety of people, here are 5 books that shaped my journey into hardware development.

If you work with the Azure(link) stack you probably want to setup some pipeline that runs your unit tests for every pull requests so you can be sure, not to merge obviously broken code.

When your project is written in javascript or typescript, Jest(link) is the go to test runner.

This post explains how to setup Jest with Azure Pipelines.

1. Add Unit Test Task

The individual tasks you want your pipeline to run are defined in a azure-pipeline.yml file.

- task: Npm@1
          displayName: run unit tests
            command: 'custom'
            customCommand: 'run test:ci'

Adding this task snippet will make your pipeline execute the npm script test:ci in the root directory of your project. If your Javascript code lives in a subdirectory you can add workingDir: 'src/myJScode' as another input.

2. Create CI-ready npm script

Next, you need to go to your package.json file in your root folder and add the test:ci npm script we already referenced in the YAML file.

"test:ci": "jest -- --watchAll=false"

By default Jest will run in watch mode. For your continuous integration pipeline however, you simply want the tests to only run once, so you need to explicitly disable that behaviour.

That's it! Now your are running your unit tests as part of your pull request workflow. If any test fails, you will know before your commit is already pushed to master.