Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing framework for node.js that specifically supports testing async code?

There are 30+ testing frameworks listed on the node.js module list...

Which frameworks are most popular? And what are their pros and cons?

I'm looking for a framework that supports unit testing of code that has a lot of async callbacks (probably like most other node projects). Also, I'm looking for something rather simple and small than complex.

So far I've looked into:

  • Nodeunit is straightforward and supports async code by letting you define how many assertions you expect to be called and when a test is done.
  • Vowsjs looks interesting. Allows you to nicely structure the tests and comes with many advanced features. Not sure whether these features are actually useful, through, or if they rather stand in your way?
like image 399
alienhard Avatar asked Mar 20 '11 17:03

alienhard


People also ask

Which framework is best for unit testing in NodeJS?

What are the best Node. js unit testing frameworks? According to “The State of JavaScript 2021,” the most popular JavaScript testing frameworks and libraries in 2021 were Testing Library, Vitest, Jest, Cypress, Playwright, and Storybook. Rounding out the top ten are Puppeteer, Mocha, Jasmine, AVA, and WebdriverIO.

What will be used for unit testing in NodeJS?

Jest is one of the most popular unit testing tools, for JavaScript in general and also for Node. js.

Which framework is used for unit testing?

Jest was the most popular JavaScript unit testing framework in 2020. For web apps that are based on React, Jest is the preferred framework. Apart from React, Jest supports unit testing of Angular, VueJS, NodeJS, and others.


2 Answers

expresso and vows are the most popular options. Although there are many good options out there. Update: As of Nov./2011, Mocha is the successor to expresso, according to the Readme.md at the expresso repository. should.js is often used with Mocha to allow BDD-style assertions.

Whatever you choose, I recommend using gently to stub core or third party modules.

I use/maintain testosterone, a little testing framework that runs test on serial so you can abuse gently. Not so many people using it though.

like image 114
masylum Avatar answered Oct 12 '22 11:10

masylum


Since March 2012 we've been using Nodeunit and haven't looked back. It's straightforward and easy to work with, it lets you choose between module-level and class-level fixtures (setup/teardown), it's got a reasonable set of built-in assertions, and its command-line tool is very flexible. Try combining it with nodemon, too.

We looked a Vows briefly but didn't want a whole new weird syntax for defining tests. All you need are assertions, fixtures, and a decent harness, and Nodeunit fits the bill perfectly.

like image 31
a paid nerd Avatar answered Oct 12 '22 13:10

a paid nerd