nodeunit icon indicating copy to clipboard operation
nodeunit copied to clipboard

Asserts not counted by reporter

Open mikebannister opened this issue 15 years ago • 9 comments

I'm curious why vanilla asserts don't get reported by nodeunit? Meaning test.ok(true) counts but assert.ok(true) doesn't get counted. Am I doing something wrong or do I need to hack something? I'd like to use something like should.js but I'd like nodeunit to track the assertions.

Thanks for any advice.

-Mike

mikebannister avatar Dec 02 '10 16:12 mikebannister

Nodeunit will report AssertionErrors from using the methods available in the assert module. However, they will not be included in the assertion count because that only includes assertions using the 'test' object passed to each test function.

Assertions are associated with a test context for a reason, since with async code it can be difficult to know which function caused the assertion to occur. Wrapping the assert methods in a new test object for each test function allows us to keep track of which test caused which assertion to fire.

caolan avatar Dec 03 '10 16:12 caolan

It might be nice if there was some mechanism to wrap other assertion libraries in the same manner. Maybe there could be some hook into nodeunit's assertion wrapper? I just feel it would be nice to plug in other assertion libraries (like should.js) to nodeunit without having to fork either. Any thoughts on this? I'm happy to do work on this but would need some input on how you think it should be done. Peace! Thanks.

mikebannister avatar Dec 03 '10 17:12 mikebannister

Yes, I think that's a good idea. There have previously been forks to add cjohansen's node-assert-extras (http://gitorious.org/node-assert-extras) too, so its not just you wanting to do this sort of thing ;)

For inspiration on where to start, take a look at the following lines in lib/types.js: https://github.com/caolan/nodeunit/blob/master/lib/types.js#L155-160

However, I'm not sure what this nicest way to hook into this is. Perhaps additional assertion modules is an option that should be added to a nodeunit.json config?

caolan avatar Dec 03 '10 17:12 caolan

Cool, seems easy enough. Any opinion on how to best get values from nodeunit.json to types#test()?

mikebannister avatar Dec 03 '10 20:12 mikebannister

No, there isn't a nice way to pass options around inside nodeunit core, only within test reporters. I'd like to fix this and I'm open to suggestions. Perhaps you could add an optional 'options' argument to the runTest etc functions?

caolan avatar Dec 05 '10 11:12 caolan

I want to use should.js with nodeunit too. Are there any progress?

craftgear avatar Feb 16 '11 13:02 craftgear

Bump. I'd like to have this too! Or should I turn to Expresso? :(

Wizek avatar Aug 11 '11 14:08 Wizek

+1

Instead of "recreating" the assert module, why not just require it and replace/extend bits of it by adding the counting method and all?

jeromegn avatar Sep 03 '11 20:09 jeromegn

Didn't find an answer, so I wrote my own hack to should.js: https://gist.github.com/simonratner/5545556

simonratner avatar May 09 '13 04:05 simonratner