Master Node.js Unit Testing with npm Scripts, Makefile, and CI

This guide walks through setting up Node.js unit testing using Mocha and Istanbul, streamlining commands with npm scripts and a Makefile, handling glob patterns, and integrating continuous integration with Travis CI or a custom UITest-ci solution.

Node Underground
Node Underground
Node Underground
Master Node.js Unit Testing with npm Scripts, Makefile, and CI
Talk is cheap, show me the code!

When you open‑source a library, writing unit tests is essential, and test coverage is a key quality metric for any library you use.

Node.js specific npm scripts

Initially, Mocha and Istanbul were installed globally, but version mismatches caused errors in team environments, so they were moved to the project’s node_modules/ directory: npm install mocha istanbul --save-dev Running tests and generating coverage required two long commands:

# Run tests
./node_modules/.bin/mocha test/*.test.js --timeout 20000
# Collect coverage report
./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -u exports --reporter spec --timeout 20000

To simplify, npm scripts were added to package.json:

{
  "scripts": {
    "test": "./node_modules/.bin/mocha 'test/*.test.js' --timeout 20000",
    "cov": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -u exports 'test/*.test.js' --timeout 20000"
  }
}

Now the short command npm run test/cov runs both testing and coverage.

Note the quoted glob pattern 'test/*.test.js'; quoting prevents the shell from expanding the pattern, ensuring Mocha receives the exact argument across different shells.

Makefile as a build tool

When many test files exist, a Makefile offers more flexibility. The following example defines test and coverage targets and allows passing a specific test file:

test := './test/*.test.js'
timeout := 20000
mocha := ./node_modules/.bin/mocha
istanbul := ./node_modules/.bin/istanbul
coverageMocha := ./node_modules/.bin/_mocha

test:
    echo 'Starting tests'
    $(mocha) --timeout $(timeout) $(test)
    echo 'Tests finished'

cov:
    $(istanbul) cover $(coverageMocha) -- -u exports $(test) --timeout $(timeout)

.PHONY: test

Running make test executes the unit tests, and make test test=test/util.test.js runs a specific file, which is harder to achieve with npm scripts alone. Note that Makefiles are not natively supported on Windows.

Continuous Integration

After local testing works, a CI system ensures that every push runs linting, tests, and coverage. Travis CI is a popular choice for Node.js, but internal repositories may require a custom solution. The author built a simple CI system called UITest‑ci that runs tests, lints code, generates coverage reports, notifies developers, and provides badge services.

Summary

The article covered the concepts and workflow of Node.js unit testing, from installing tools and simplifying commands with npm scripts and Makefiles to integrating continuous integration, setting the stage for deeper topics like supertest for API testing, testing private methods, and testing CLI tools.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Node.jsunit testingcontinuous integrationMakefilenpm scripts
Node Underground
Written by

Node Underground

No language is immortal—Node.js isn’t either—but thoughtful reflection is priceless. This underground community for Node.js enthusiasts was started by Taobao’s Front‑End Team (FED) to share our original insights and viewpoints from working with Node.js. Follow us. BTW, we’re hiring.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.