Tutorial: Set up a continuous testing pipeline with Node.js

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Source:- assertible.com

Continuous integration is large part of modern development workflows. Most of us are familiar with basic CI services that automatically build code as new changes are pushed. But what if you took that a step further and not only built your code, but also deployed and tested it within the same pipeline? This is continuous development.
Continuous development pipeline

Let’s outline the steps of the pipeline:

  • Continuous integration: Push new code to GitHub, which is then built by CircleCI.
  • Continuous deployment: Automatically deploy the new application to Heroku.
  • Continuous testing: Execute post-deployment tests with Assertible.

I’ve created an example Node.js app for the code in this post. In the end, you’ll have a workflow that will automatically build, deploy, and test your app after every push.

Continuous integration with CircleCI

The pipeline is initiated when code is pushed to a repository on GitHub. CircleCI will automatically pull the new changes, build the code, and run the unit tests. Setting this up only takes two steps:

  1. Add a circle.yml to our repository. You can see the example app’s config file here.
  2. Tell CircleCI to watch the project. This is done in your CircleCI dashboard, by adding projects

The .circle.yml file contains the commands used to build the code. Since the example app is written in Node.js, the default settings will work (npm install) and you don’t need to do anything else.

Now, every time code is pushed to the GitHub repository, CircleCI will run the unit tests and fail if there are any issues:
CircleCI successful build

Continuous deployment with Heroku

After our code has been built and the unit tests have passed, CircleCI will look for a deployment step in our configuration. In the example app, I’m deploying the application to Heroku.

In the example project’s circle.yml file, take a look at the deployment step again:

deployment:
  production:
    branch: master
    commands:
      # ...removed lines for brevity
      # Deploy the app to heroku
      - git push git@heroku.com:assertible-nodejs-example.git $CIRCLE_SHA1:refs/heads/master

This configuration tells CircleCI to deploy our application to production when you push to the master branch of the repo. The deployment is executed by running each step in commands.

This is continuous deployment: every time changes are pushed to the repo, CircleCI will build the app, and the deploy it to Heroku. Automatically. Every time. That’s pretty cool!
Heroku deployment activity

For more on deploying to Heroku, check out the Continuous deployment with Heroku docs on CircleCI or Continuously testing Heroku GitHub deployments blog on Assertible.

Continuous testing with Assertible

After the application is deployed, you want to run an automated test suite on the new version to verify the release ensure it meets a certain standard. To do this, you use Assertible.

Let’s look at the last couple of lines in our circle.yml after deploying to Heroku:

# Download github_deploy script
- curl https://raw.githubusercontent.com/assertible/deployments/master/github_deploy > github_deploy
- chmod +x github_deploy

# Send a deployment event to GitHub. GitHub will then send that
# event to Assertible, and Assertible will run your API tests.
- |
  APPROOT=https://assertible-nodejs-example.herokuapp.com/
  DEPLOY_ID=$(ENVIRONMENT_NAME="production" ./github_deploy $CIRCLE_SHA1 $CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME $APPROOT "pending")
  ./github_deploy $CIRCLE_SHA1 $CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME $APPROOT "success" $DEPLOY_ID

This code uses the github_deploy script to send “deployment” events to our GitHub repository. Assertible watches your repo for these events, and when a successful deployment occurs, your API tests will automatically be executed. This is post-deployment testing, a part of automated QA testing.

In the image below, you can see that Assertible tested the new deployment and ran the defined assertions on the application. It even says what commit the deployment was from:
Post-deployment API testing result

Connecting Assertible to GitHub is easy! Just follow our GitHub Quick Start Guide.

Continuous testing is important for any team or developer releasing software. Automating API and QA tests not only saves time, but covers more cases faster than traditional, manual testing methods.

Testing staging environments from Pull Requests

A lot of times you’re not deploying to production, but rather to a staging environment or Heroku review app. Assertible knows about your different environments and will run your API tests on the correct application.

When you deploy to a staging environment from a GitHub pull request, Assertible will show a status check that ensures all of your API tests are passing before the new changes are merged. Awesome!
GitHub status checks for continuous testing

Resources and examples

Even more automation and integrations

There’s even more you can automate. Heroku has a ton of addons you can use to automate various parts of your build. Assertible supports integrations like Zapier that can be used to customize how you use your test results and get alerts of failures.

Integration with other CI services

Nothing about this pipeline is specific to GitHub, CircleCI, Heroku, or even Assertible. Learn more about setting up this pipeline with different CI providers, like Wercker, in the assertible/deployments repo.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x