Set up Automated Testing using GitHub Actions.

Bilal K.
3 min readMar 10, 2023

GitHub Actions is a powerful and flexible CI/CD automation platform that allows developers to build, test, and deploy software applications right from their GitHub repositories.

Photo by Rubaitul Azad on Unsplash

We often forget to run the test cases before merging the code to the main branch. If the committed code is faulty and does not pass all the test cases written, the code base will break and can lead the team to big problems in production.
In this post, we’ll learn how to use Github Actions to run the unit tests before creating a new pull request to see if all the tests are passed in the code. This will save time, and nothing broken will be pushed to the branch. Let’s dive in.

Approach.

  • define the trigger point, when to run the CI workflow (eg: pull_request).
  • define the node version.
  • Retrieve the cache to speed up the process.
  • Install the dependencies if not cached.
  • Run the tests.

To get started, add the starter workflow to your repository’s .github/workflows directory.

So what did we just do above? We’ll walk through the configuration file and understand what we’re actually doing.

  • Starting from the top, we first specify our name using: name: Github-CI
  • on: [push, pull_request]: The -on key is how we specify what events trigger our action. Here, we're simply saying that we want this action to run someone pushes the commits to any branch or someone creates a pull request.
  • runs-on: ubuntu-latest& node-version: [16.x]: Let’s declare the machine and node version on which we want to run the tests. I’m running the script on the latest Ubuntu version and the Matrix Node version is set to 16.x. We can run the tests on multiple node versions to make sure everything is working and cross-compatible with different systems.

Finally, we specify the steps we want our job to run.

  • uses: actions/checkout@v2: This checks out our code on our job environment is available so we can use it to run tests.
  • uses: actions/setup-node@v3: Since we’re using node with our project, we’ll need it set up on our environment. We’re using this action to do that setup for us for each version we’ve specified in the matrix we configured above.
  • run: npm install -g yarn& run: yarn install — frozen-lockfile: We will install the yarn and other project dependencies. We’ve declared the flag frozen-lockfile to make sure the correct version of the required dependencies gets installed. The if flag performs what it sounds like and only runs this command if the required dependencies are not found in the cache.
  • run:npx nx test api — bail — parallel — maxWorkers=2: Now run the test script in our project with bail parallel and maxWorkers flags for optimization

So that’s it! I hope you found it helpful. You can learn more about Automated build and test from official docs here.

Till next time :) Happy Learning!

Bilal K.

--

--

Bilal K.

Software Engineer, I share tips and tools to help fellow developers level up.