Configuration file: Setup

Setup function

If you need to configure your testing framework or sandbox environment before a test run starts, you can use the setup setting (or its alias bootstrap). This allows you to specify a function to be executed just before a test run starts.

 module.exports = function () {
   return {
     files: [
       'src/**/*.js'
     ],

     tests: [
       'test/**/*Spec.js'
     ],

     setup: function (wallaby) {
       // wallaby.testFramework is jasmine/QUnit/mocha object
       wallaby.testFramework.ui('tdd');

       // you can access 'window' object in a browser environment,
       // 'global' object or require(...) something in node environment
     }
   };
 };

The parameter of the function (named wallaby in the code above) has the following functions and properties:

  • delayStart() function for preventing wallaby from starting the test run automatically (e.g. when you want to perform some async actions before starting the test run). You are responsible for calling the start() method (see below).
  • start() function for starting the test run manually when delayStart() is used.
  • tests property that contains the list of all test files that wallaby.js knows about.
  • testFramework property that contains a reference to the test framework object. The property can be used to configure your test framework before executing tests, for example to change some of its default settings.

The following additional properties are available in node.js environments:

  • localProjectDir string property returns the project local folder.
  • projectCacheDir string property returns the project cache folder. Note that wallaby.js uses files from this folder to run tests. It copies the files specified in files and tests lists from localProjectDir to projectCacheDir.
  • workerId numeric property returns the id of a worker that runs tests. The number is always between 0 and MAX(worker.initial, workers.regular) - 1.
  • sessionId is a unique identifier of a test run.

Important: The setup function is executed in the context of the test runner, not in the context of the configuration file.

This means that you cannot use objects/variables defined within the configuration file code. For example, this code:

module.exports = function () {
  var myParam = 'hello from config';
  return {
    files: [
      'src/**/*.js'
    ],

    tests: [
      'test/**/*Spec.js'
    ],

    setup: function (wallaby) {
      console.log(myParam);
    }
  };
};

will output undefined, because myParam will not be defined in the context of the test runner, when the setup function will be executed.

However, because the setup function is executed in the context of the test runner, you can use its environment to initialize it the way you need.

For example, you can access the window object if you are running browser tests or use the global object in case of node.js tests (or the require modules you need).

For a typical scenario of configuring a testing framework before the test run, for example mocha options, the configuration may look as follows:

module.exports = function () {
  return {
    files: [
      'src/**/*.js'
    ],

    tests: [
      'test/**/*Spec.js'
    ],

    setup: function (wallaby) {
      var mocha = wallaby.testFramework;
      mocha.ui('tdd');
      // etc.
    }
  };
};

Teardown function

Please note that the feature described below works only for node.js test runner.

Sometimes you may need to clean up after your tests, and it may be something that you may have set up in the setup function and not in your testing frameworks before/after hooks. To do that, wallaby.js support the teardown function:

module.exports = function () {
  return {
    files: [
      'src/**/*.js'
    ],

    tests: [
      'test/**/*Spec.js'
    ],

    setup: function (wallaby) {
      console.log('Setup');
      console.log('Current worker id: ' + wallaby.workerId);
      console.log('Current session id: ' + wallaby.sessionId);
    },

    teardown: function (wallaby) {
      console.log('Teardown');
      console.log('Current worker id: ' + wallaby.workerId);
      console.log('Current session id: ' + wallaby.sessionId);
    }
  };
};

The teardown function will only be invoked if the setup was invoked.