Practical but powerful way to test Node.js APIs without hassle


By:
Santiago Quinteros

Practical but powerful way to test Node.js APIs without hassle

If you are not using any kind of automated testing suite for your node.js apis, you are insane.

(No, I'm not gonna sell you a new javascript framework)

Hey, this is not another generic post of how to configure mocha or something (I think jest is what the kids use these days)

Instead, I'm gonna provide you a life-saver guide on how to add Integration tests to an existing API no matter how big or complex it is, and by using tools that you already have.

But also, I'm gonna provide you a free and open-source tool that I build for this purpose, so, keep reading.

The problem

I was working on a big project alongside 4 other backend developers and about 60% of the time we were fixing simple bug caused by accidentally changing the API Responses.

And nobody notices it until the bug was already in production.

This happened because we were in a constant rush deploying new features and we didn't dedicate the appropriated time to QA.

Yeah, I know, it is our fault anyways.

Many times we tried to add unit tests to our codebase but it was too hard to decouple the ugly and old code one of my source of inspiration for the infamous bulletproof architecture

In any case, what I'm gonna show you is a life-saver, and I always add it to a new project and recommend this when I'm hired as a consultant.

It's just Postman

If you are a backend developer, chances are that you already know about this tool, but here is a quick introduction:

Postman is a popular API client that makes it easy for developers to create, share, test and document API

We are gonna take advantage of Postman test tab in a way that you may never have heard about, so keep reading.

A million-dollar feature hidden in plain sight

I'm exaggerating of course

The best to solve my problem of missing or inconsistent API response was to assert the existence and type of the JSON fields.

If you think about it, is not so difficult to create a script that transforms a JSON structure to a generic Postman assert test.

And that's what I did. Then I just added a simple web UI for it.

Tests Pass Postman Test Tab

nicer styles will come later

Check it out at https://autotester.softwareontheroad.com/

It is damn simple to use, just past your JSON API Response on the left side, and click on the arrow symbol to generate your Postman's tests

In less than 3 hours I added tests to 90% of our endpoints and created tests for 10 use cases (or user flows, whatever)

Tests Pass Postman Test Tab

Taking it to the next level - Continuous Integration

Did you know that Postman has a brother?

Introducing Newman the CLI version of Postman.

Newman is a command-line collection runner for Postman. It allows you to effortlessly run and test a Postman collection directly from the command-line. It is built with extensibility in mind so that you can easily integrate it with your continuous integration servers and build systems.

With Newman, we can run our collections inside a terminal environment. So, that means we can run it inside a Continuous Integration tool!

How to use it:

npm install -g newman

newman run examples/sample-collection.json

Newman runner example

I like to use CircleCI, but the process is almost identical for other CI tools like Github Actions, Jenkings, or TravisCI.

version: 2
  "integration-test":
    docker:
      - image: circleci/node:12.18.2-buster
      - image: circleci/mongo:4.2
    working_directory: ~/repo
    steps:
      - checkout
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-
      - run: npm install
      - run: sudo npm install -g newman
      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}
      - run:
          shell: /bin/sh
          command: wget --retry-connrefused --waitretry=1 --read-timeout=20 --timeout=15 -t 10 http://localhost:8080
      - run:
          command: newman run ./tests/integration/softwareontheroad.com.postman_collection.json -e ./tests/integration/softwareontheroad.com.postman_environment.json
          environment:
            MINUTES_RATE_LIMIT: 1
            ATTEMPTS_RATE_LIMIT: 2

  "deploy":
    docker:
      - image: circleci/node:12.18.2-buster
    working_directory: ~/repo
    steps:
      - checkout
      - run:
          name: Deploying Server to Heroku...
          command: |
            npm run deploy:heroku


workflows:
  version: 2
  build_and_deploy:
    jobs:
      - integration-test
      - deploy:
          requires:
            - integration-test
          filters:
            branches:
              only:
                - production
                - master

Conclusion

We took advantage of a not so spoken feature of Postman, the test tab.

Then we created simple assert tests for our API endpoints - we can use this tool that I created for this article.

Lastly, by using Newman we run our tests inside a CI environment.

Now you can go and ask your manager for a rise!

I wish postman has any kind of affiliate program, I talk very well about it.

But this is my way of paying them back, thanks for all of these years of saving my ass. Sam.

Get the latest articles in your inbox.

Join the other 2000+ savvy node.js developers who get article updates. You will receive only high-quality articles about Node.js, Cloud Computing and Javascript front-end frameworks.


santypk4

CEO at Softwareontheroad.com