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.
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.
What You Will Learn:
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 for 2017.
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.
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:
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.
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.
Step #2: Setup your package.json to include a test script.
Step #3: Create some tests.
(Click on the image for enlarged view)
Step #4: Execute your tests by typing ‘npm run test’ in a command line.
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.
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.
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 wakecoder.com or github.com/wakecoder.
Please share your comments, questions, and experiences with us below.