How to perform unit tests on nodejs

Asked

Viewed 2,138 times

15

I’d like to run unit tests on Node.js, I’m using the grunt-jasmine, but it does not recognize the variable exports, module and neither required.

Is there a way to solve this or does someone suggest a more suitable testing framework for Node? I’m using it in conjunction with Grunt.

My settings:

jasmine: { src: 'src/**/*.js', options: { specs: 'specs/**/*Spec.js' } }
  • I use Grunt-Jasmine, which uses Jasmine-Node.

  • Just to be boring: The correct is "unit test", not "unit test".

3 answers

9


Assert

One of the Node modules is the assert. It is not a complete tool for unit testing but it is possible to use it without any additional to perform its tests.

Methods

  • fail: Compares two values (style current and expected).
  • ok: Check if the expression passed is true.
  • throws, doesNotThrow: The function passed should/should not launch an exception.
  • ifError: Tests if past value is true; useful for testing error variable.

In addition to several methods Equal: Equal, notEqual, deepEqual, notDeepEqual, strictEqual, notStrictEqual.


Nodeunit

Based on the assert explained above, nodeunit is one of the simplest options to write your unit tests. It supports asynchronous tests.

Methods

The methods for testing are the same as the assert described above. In addition, for each test function the nodeunit sends you an object with the following functions:

  • expect(quantidade): Number of assertions that will be made in this test. If a number of different assertions from the past are made the test fails. Calling this function is optional.
  • done: Ends the current test. This function must be called.

Writing the test module

To write a test module is very simple, just expose your test functions and receive in them a parameter with the object test nodeunit, for example:

exports.testaAlgo = function (test) {
    test.expect(2);
    test.ok(true, "este teste irá passar");
    test.ok(false, "este teste irá falhar e essa mensagem será exibida");
    test.done();
};

Running the tests

Just install nodeunit via npm:

npm install -g nodeunit

And then run the test:

nodeunit meuTeste.js

The exit is something like:

$ nodeunit meuTeste.js
✖ testaAlgo

Assertion Message: este teste irá falhar e essa mensagem será exibida

Configuring unit tests in the project

One of the ways to configure your tests in your project is to use the package json., example:

"scripts": {
  "test": "nodeunit testes/*.js"
}

This way just run npm test to run your tests.

Not to have to install nodeunit globally on your machine (paramêtro -g of npm install), you can use the local node of the *node_modules folder*:

"scripts": {
  "test": "./node_modules/.bin/nodeunit testes/*.js"
}

6

I won’t be answering your question, but there are other testing alternatives for Node.js.

If you’re looking for something more TDD-style:

If you prefer the BDD style, like Jasmine:

I met these modules and frameworks by reviewing the book Node.js in Action. I recommend the book.

  • OK. I will use nodeunit through the Grunt-contrib-nodeunit plugin as I use Grunt for the project build. Thank you very much

  • As you solved my question, and I decided to use another testing framework (as you suggested), I changed the title and I will give OK in your reply.

  • Thank you, Daniel. Although they are not officially supported there are also Grunt plugins for Mocha and pro Vows. If you update your profile with email, we can exchange some ideas as well. Hug.

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.