Is there a way to tell if nodeunit has completed all tests?

I need to run the code after nodeunit

all tests have passed successfully. I am testing some wrappers Firebase

and reference blocks Firebase

exiting nodeunit after running all tests.

I am looking for some hook or callback to run after all unit tests have passed. So I can end the process Firebase

so I nodeunit

can exit.

+3


source to share


4 answers


Didn't find the right way to do this.

There is my workaround:

//Put a *LAST* test to clear all if needed:
exports.last_test = function(test){
    //do_clear_all_things_if_needed();
    setTimeout(process.exit, 500); // exit in 500 milli-seconds    
    test.done(); 
} 

      



In my case this is used to make sure the DB connection or any network connection is killed in any way. The reason it works is because nodeunit runs tests in sequence.

This is not the best, not even the best way, just to get the test out .

For nodeunit 0.9.0

+4


source


For a recent project, we counted tests, iterations exports

, and then called tearDown

to count completions. After the last tests, process.exit () is called.

See specification for details . Note that this was at the end of the file (after all tests were added to the export)



(function(exports) {
  // firebase is holding open a socket connection
  // this just ends the process to terminate it
  var total = 0, expectCount = countTests(exports);
  exports.tearDown = function(done) {
    if( ++total === expectCount ) {
      setTimeout(function() {
        process.exit();
      }, 500);
    }
    done();
  };

  function countTests(exports) {
    var count = 0;
    for(var key in exports) {
      if( key.match(/^test/) ) {
        count++;
      }
    }
    return count;
  }
})(exports);

      

+2


source


According to the nodeunit docs, I can't seem to find a way to provide a callback after all tests have run.

I suggest you use Grunt so that you can create a test workflow with tasks like:

  • Install the command line tool: npm install -g grunt-cli

  • Install grunt to your project npm install grunt --save-dev

  • Install the nodeunit grunt plugin: npm install grunt-contrib-nodeunit --save-dev

  • Create Gruntfile.js like this:

    module.exports = function(grunt) {
    
        grunt.initConfig({
            nodeunit : {
                all : ['tests/*.js'] //point to where your tests are
            }
        });
    
        grunt.loadNpmTasks('grunt-contrib-nodeunit');
    
        grunt.registerTask('test', [
            'nodeunit'
        ]);
    };
    
          

  • Create your custom task to run after the tests by changing the grunt file to the following:

    module.exports = function(grunt) {
    
        grunt.initConfig({
            nodeunit : {
                all : ['tests/*.js'] //point to where your tests are
            }
        });
    
        grunt.loadNpmTasks('grunt-contrib-nodeunit');
    
        //this is just an example you can do whatever you want
        grunt.registerTask('generate-build-json', 'Generates a build.json file containing date and time info of the build', function() {
            fs.writeFileSync('build.json', JSON.stringify({
                platform: os.platform(),
                arch: os.arch(),
                timestamp: new Date().toISOString()
            }, null, 4));
    
            grunt.log.writeln('File build.json created.');
        });
    
        grunt.registerTask('test', [
            'nodeunit',
            'generate-build-json'
        ]);
    };
    
          

  • Run test tasks with grunt test

0


source


I came across another solution how to deal with this solution. I have to say that all the answers here are correct. However, when checking out grunt, I found out that Grunt runs nodeunit tests through the reporter, and the reporter offers a callback option when all tests are finished. You can do it something like this:

in folder

test_scripts/
   some_test.js

      

test.js might contain something like this:

//loads default reporter, but any other can be used
var reporter = require('nodeunit').reporters.default;
// safer exit, but process.exit(0) will do the same in most cases
var exit = require('exit');

reporter.run(['test/basic.js'], null, function(){
    console.log(' now the tests are finished');
    exit(0);
});

      

it is possible to add a script to say package.json

in the script object

  "scripts": {
    "nodeunit": "node scripts/some_test.js",
  },

      

now it can be done like

npm run nodeunit

      

tests in some_tests.js can be coded or can be run one by one using npm

0


source







All Articles