caolan / nodeunit

Easy unit testing in node.js and the browser, based on the assert module.
MIT License
1.9k stars 370 forks source link

Q: Is there a better way to dynamically create tests than a monkey-patch? #340

Closed MaxMotovilov closed 7 years ago

MaxMotovilov commented 7 years ago

I am thinking of implementing a combinatorial sequence enumeration algorithm for asynchronous tests (exhaustive reachability testing). For that to work in the context of nodeunit, I need to generate multiple test cases dynamically from a single test function -- the total number of test cases to run cannot generally be determined until the test code executes at least once, possibly multiple times. Monkey patching core.runTest() is the only approach I came up with so far; is there a better way?

Example (runs myTest twice):

const assert = require( "assert" ),
      core = require( "nodeunit/lib/core" );

core.runTest = function( runTest ) {
    return function( name, fn, opt, callback ) {
        runTest( 
            name, fn, opt, 
            () => runTest( name + "-2", fn, opt, callback ) 
        )
    }
}( core.runTest )

exports.myTest = function( test ) {
    assert.equal( "foo", "foo" );
    test.done();
}
mreinstein commented 7 years ago

It's an interesting feature idea, but this is outside the scope of nodeunit's testing system. Thanks for sharing though!