How to Setup the Node.js Testing Framework: Node.js Tutorial

How to Setup a testing framework in Node.js – A short tutorial for beginners

Why Does it Matter?

Obviously, testing is important – without it, bugs sneak into the wild where they’re more difficult and costly to fix. Automating testing can significantly increase test coverage as well as reduce long-term costs.

In some teams, this falls to the developers but it can also fall to testers to create automated tests. These automated tests can be unit tests (concise tests that target very small pieces of functionality) or larger, integration-level tests.

Node.js Tutorial

Either way, the task of identifying and integrating tools for automated testing in a Node.js environment can be daunting.

In this post, we briefly review several popular tools and provide an overview of how these tools should be integrated together to form a comprehensive test environment.

Which Frameworks Should I Use?

While there are several testing tools and frameworks available (and new ones under development), we’ve used and abused many of them to bring you this list of the best testing tools.

#1) Mocha

Mocha is an excellent testing framework that allows for use of promises and asynchronous/await with TypeScript or Babel. Mocha handles executing the tests you create, catches any assertion errors and pretty-prints these to the console.

#2) Chai

Chai is an assertion library that allows you to use natural language constructs when developing your tests. This is extremely helpful as many assertion libraries can be rather cryptic.

The following is an example that illustrates how naturally assertions can be written with Chai:

expect (myResult).to.equal(23)

#3) Mockery

Mockery is a small npm module that allows you to substitute test mocks without modifying your production code in any way. By simply creating a mock function or module and registering it with mockery, Node.js will inject your mocks wherever a require statement is used in your code.

#4) Jenkins

Jenkins is a continuous integration system that can hook into your version control (e.g. git) and automatically execute mocha any time a commit occurs. This means your product is being tested every time a change occurs.

How do I setup a testing framework in Node.js?

Step #1: Add mocha, chai, and mockery as dependencies to your project.

add mocha, chai and mockery

Step #2: Setup your package.json to include a test script.

Setup your package

Step #3: Create some tests.

(Click on the image for enlarged view)

Create some tests

Step #4: Execute your tests by typing ‘npm run test’ in a command line.

Execute your tests

Where Can I Find More?

As you can see, Mocha and Chai provide an excellent testing experience with a very low barrier to entry.

In our follow-up post, we’ll show you how to use Mockery, how to write asynchronous tests in Mocha and discuss how to configure all of this in Jenkins.

To play around with our example code, please visit GitHub. 

Suggested reading => How to set up Consumer Pact Test


In this Node.js tutorial, we briefly reviewed several popular JavaScript testing frameworks and provided an overview of how these frameworks should be integrated together to form a comprehensive test environment.

Though we have many testing tools and framework available in the current market, by going through this article we can easily learn how to set up the Node.js framework.

Further reading =>> Frequently asked Node.js Interview Questions and Answers

About the Author: Dave Beck has a M.S. in Computer Science and spends way too much time doing software development. When he’s not writing code he likes to lift weights and wakeboard. You can find him online at or

Suggested reading =>> D3.js Tutorial For Beginners

Please share your comments, questions, and experiences with us below.

Related Post

Leave a Reply

Your email address will not be published.