How to setup automated testing in nodejs using github actions
Tags: githubactions, testing, node, backend
Published: 2023-03-02 11:04:42

Last time out, we set out to understand github actions, its uses as well as how to set up an [action](https://dev.to/alahirajeffrey/understanding-github-actions-an-introduction-15g6) 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](https://github.com/alahirajeffrey/github-action-tutorials). Kindly leave a like. That being said, a snippet of the structure of the repo can be found below. 

![project structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3f5oovhvmutqrtlvo65g.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8stvpe1iunc8lxnbbsgb.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3upinkj6xb1x88t3sg0h.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/knyjvqzexann6b6ucahw.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h19s2myelb8gt6crnb8b.PNG)

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.