Learn How to Test Serverless Applications
Tags: serverless, javascript, devops, tutorial
Published: 2023-03-27 05:59:49

Serverless has revolutionized the way applications are developed and deployed. With serverless architecture, developers can build and run applications without worrying about the underlying infrastructure. Node.js, a popular open-source platform, has become a go-to choice for building serverless applications due to its flexibility and scalability. Node.js helps developers build serverless applications quickly and efficiently. It offers an event-driven, non-blocking I/O model that makes it lightweight and ideal for microservices architecture. With Node.js, developers can build event-driven applications that can handle a large number of requests at once. It also offers a rich set of modules and libraries that make it easy to build complex serverless applications. 

Today, we will see how to build an automated continuous integration pipeline to test our sample Node.js serverless application. Let's go!

## Prerequisites
- [Free Harness account](https://app.harness.io/auth/#/signup/?module=ci&utm_source=internal&utm_medium=social&utm_campaign=devadvocacy&utm_content=pavan_serverless_article&utm_term=get-started) to automate your tests
- Sample Node.js serverless application. We have already created a [sample serverless application](https://github.com/pavanbelagatti/serverless-example.git); feel free to fork the same and use it.

## Tutorial
First, [sign in to your Harness account](https://app.harness.io/auth/#/signup/?module=ci&utm_source=internal&utm_medium=social&utm_campaign=devadvocacy&utm_content=pavan_serverless_article&utm_term=get-started). Harness is a continuous delivery platform. 
![signup Harness](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6lyk0wxigi9a2kqak2fi.png)

Get started with creating your first pipeline
![first pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6rgri1gpcbmbt8q2jx1.png)

Authenticate with your GitHub account as your application code is present there.
![add github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pcdkkrm132wok2r37his.png)

![authenticate with GitHub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1efbg84t05szpe9lh1c.png)

Select your application repository. The serverless application you forked above.
![serverless application](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnhsek3bdv6lqn3k13ym.png)

Select 'Node.js' from the list and start configuring the pipeline. The right side in the below screenshot you see is a default yaml configuration of your pipeline.
![default yaml](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xsz9qyxhad2od4tqof1m.png)

Once you continue, you will land on the pipeline studio which looks as below
![build node app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oiyfzl7uclet2n0r25p1.png)

Under the execution, click 'Build Node App' and you will see the default settings and commands.
![Build Node App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aprs08ab23d66m4k4lt4.png)

The commands you see are below,

```
echo "Welcome to Harness CI"
node --version
npm install
npm run build --if-present
npm test
```

You can modify and retain only the last three commands if you want.

Apply changes, save everything and run the pipeline.
![run pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pftj89fowp2lifimf8m6.png)

You should see a successful pipeline execution.
![successful pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gmnxm8s3vobpf1jfiw97.png)

Switch to the console view and you should see what's happening in each stage.
![console view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmxoc4vj0jn85uxqtm4e.png)

Your pipeline is all automated to run tests whenever any developer pushes code to the main branch.
You can verify this by going to the 'Triggers' tab from the pipeline studio.
![Triggers tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77vc098k76bzl4hefahf.png)

You can see push and pull triggers already configured for you.
![push pull triggers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtcemvfb1x6wg3ft24oa.png)

Let's push some code to the main branch.
As soon as you push the code, the build gets triggered and you can see the same below.
![pipeline triggered](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzlj7uw33g1qdbjm412g.png)![code triggered CI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/598r8z1rezhrdl71u4sa.png)

**_Congratulations!_** We have successfully configured a simple continuous integration pipeline for a sample Node.js serverless application. 

If you like to learn a step-by-step CI/CD set-up, take a look at my complete CI/CD tutorial.
{% embed https://dev.to/pavanbelagatti/learn-how-to-setup-a-cicd-pipeline-from-scratch-for-a-go-application-4m69 %}