DEV Community

Alahira Jeffrey Calvin
Alahira Jeffrey Calvin

Posted on • Edited on

How to setup automated testing in nodejs using github actions

Last time out, we set out to understand github actions, its uses as well as how to set up an action that simply printed "hello world". However, while it helped us understand the concept of github actions, in a real-world application, it would not be very useful. A particularly important use case, on the other hand, would be trying to run tests once a pull request is made to ensure no breaking changes are made when the pull request is merged and deployed

The code used in this post can be found here. Kindly leave a like. That being said, a snippet of the structure of the repo can be found below.

project structure

First and foremost, our index file which is located in the src folder contains four basic functions which add,
subtract, divide, and multiply 2 numbers and return the values. Tests for the functions in the index file
are found in the test file which is located in the tests folder. The package.json file contains the metadata and configurations of the project as well as the command which runs the test. This command can be found in the scripts section of the file and can
be run by simply typing npm run test in the terminal.

The file containing the workflow which is where this post is mostly focused on can be found by navigating to the
.github/workflows folder.

workflow to run tests in nodejs

Lines 1 - 4 above specifies the name of the workflow as well as the conditions for the workflow to run i.e. the event that triggers the workflow which occurs when a pull request is made to the master branch.

name of job

The snippet above contains the name of the job "run-test" as well as the operating system of the runner which is used to
run the code i.e. the lastest version of ubuntu available.

github actions workflow steps

A workflow job is made up of a series of steps. The steps involved in this job can the found above. The first step involves
checking out of the repository so the job can access it. The second step sets up the nodejs environment in the runner
using already defined actions which can be found in the github actions marketplace. This is because the application being
run is a nodejs application. If a python application was being run, a python environment would be set up.

Lines 16 and 17 specify the node version to use. The third and fourth steps involve installing the dependencies as well as
running the test specified in the package.json file consecutively.

Below is a result of the workflow which can be found by navigating to the actions tab of the repo on github.

workflow result

In this part of the github action series, we learned how to set up a CI pipeline to run tests using github actions. In the
next post, we would explore how the set up a CI pipeline that runs tests that require a database. We would make use of
docker-compose to spin up a test Postgres database while our action is running and run tests.

Top comments (0)