Merc
Merc

Reputation: 17087

Abort tests with nodeunit

I am writing unit tests for a database driver. In the first test, I create some object that I then intend to use later in the tests. I would like to write the tests so that if this (crucial) step fails, everything else just stops. I did this, but it feels a little too hacky... what are the best practices in this case?

"create constructors and layers": function( test ){
  var self = this;

  try {
    self.shared.personLayer = new self.Layer( 'people', {  name: true, surname: true, age: true } );
    test.ok( self.shared.personLayer );

    self.shared.personLayerNoDb = new self.LayerNoDb( 'people', {  name: true, surname: true, age: true }, self.db );
    test.ok( self.shared.personLayerNoDb );

    var personLayerOverwriteDb = new self.Layer( 'people', {  name: true, surname: true, age: true }, self.db );
    test.ok( personLayerOverwriteDb );

    self.shared.ranks = new self.Layer( 'ranks', {  name: true, number: true } );
    test.ok( self.shared.ranks );

  } catch( e ){
    console.log("Error: couldn't create layers, aborting all tests...");
    console.log( e );
    console.log( e.stack );
    process.exit();
  }

  test.done();
},

Upvotes: 0

Views: 107

Answers (1)

Ray Toal
Ray Toal

Reputation: 88468

Any "crucial step" that is required for all other tests to take place is not a unit test. Unit tests are supposed to be independent of each other, and in principle can be run in any order.

What you are looking for is a "Before" or "Before Each" hook.

Place your setup code in the setUp section, documented on the nodeunit README

Upvotes: 1

Related Questions