How to perform unit tests on nodejs

14

I would like to do unit tests on Node.js , I am using grunt-jasmine , but it does not recognize the variable exports , module and nor required .

Is there a way to solve this or does someone suggest a testing framework more appropriate to the node? I'm using it in conjunction with grunt .

My settings:

jasmine: { src: 'src/**/*.js', options: { specs: 'specs/**/*Spec.js' } }
    
asked by anonymous 11.12.2013 / 19:28

3 answers

9

Assert

One of the node modules is assert . It is not a complete tool for unit testing but you can use it without any additional tests.

Methods

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

In addition to several equal methods: equal , notEqual , deepEqual , notDeepEqual , strictEqual , not html

Nodeunit

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

Methods

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

  • expect(quantidade) : The 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 should 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 test object of the nodeunit, eg:

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 the nodeunit via npm:

npm install -g nodeunit

And then run the test:

nodeunit meuTeste.js

The output 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 way to configure your tests in your project is to use package.json , for example:

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

In this way you just run npm test to run your tests.

To not have to install the nodeunit in a global way on your machine (parameter -g of npm install ), you can use the local nodeunit of the * node_modules *:

"scripts": {
  "test": "./node_modules/.bin/nodeunit testes/*.js"
}
    
11.12.2013 / 22:43
6

I will not be answering your question, but there are other testing alternatives for node.js.

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

  • the assert module embedded in node.js
  • nodeunit (which makes use of the assert module) - link

If you prefer BDD style, like Jasmine:

  • has Mocha (serves both TDD and BDD) - link
  • Vows (cool) - link
  • should.js (also uses assertion to do BDD) - link

I've come across these modules and frameworks by reviewing the book Node.js in Action. I recommend the book.

    
11.12.2013 / 20:46
1

I recommend using link and link for unit tests using the TDD and BDD style for complete and effective testing. If you also need to test internal or external APIS, use github.com/visionmedia/supertest

    
27.02.2014 / 00:23