1

In my NodeUnit tests, I have such a piece of code:

exports['aTest'] = function(test){
...
var functionResult = test.doesNotThrow(aFunction(aParam));
...
}

But functionResult is undefined after the call to doesNotThrow(...) Why does test.doesNotThrow() not return the result of the function call? That would be quite elegant.

lOlive
  • 209
  • 1
  • 5

1 Answers1

0

It doesn't return anything as shown in the source code, this is because nodeunit relies on AssertionError to determine if a suite succeeded or not, as you could see here

Why would you want it to return something, what return value would you expect?
The purpose of the library is to test your assertions. It should not return a value since that would mean giving you info to do something with that value, instead, when an assertion throws you will get a console message saying that your tests got broken. I believe this is a good approach.

Your goal is to be notified when something brokes, relying on the library to tell you what broke, not to delve yourself into the details, that is why I believe the author chose this way.

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
  • Returning undefined is strictly inferior to returning the result of the call doesNotThrow(...) has made. – lOlive Jul 15 '13 at 15:10
  • That is because this is unit testing, you are testing that the function does not throw, in another test you will test for a result and see if it matches your expectations. – Alberto Zaccagni Jul 15 '13 at 15:16
  • I am wondering why chaining assertions is considered a bad practice. cf this post http://stackoverflow.com/questions/2430429/are-multiple-asserts-bad-in-a-unit-test-even-if-chaining – lOlive Jul 15 '13 at 15:21
  • Because you are testing multiple things at once, it's called unit testing because you test single units of logic, each on its own. – Alberto Zaccagni Jul 15 '13 at 15:29
  • Test one feature per test, don't try to test a lot of them together. I advice you to open a new question if this didn't answer. Or if it did please upvote/mark as accepted. – Alberto Zaccagni Jul 15 '13 at 15:44