Setup function

If you’d like to configure your testing framework or sandbox environment just before a test run starts, you can use the setup setting (or its alias bootstrap). It allows you to specify the 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:

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

The API will be extended in the future to include more members. Feel free to request more wallaby data or control functions to be exposed via the parameter.

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

It 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.