Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

whiskey

cloudkick5650.8.4

Whiskey is a powerful test runner for Node.js applications and a process orchestration framework which makes running integration tests with a lot of service / process dependencies easier.

whiskey, tests, test runner, testing, tdd, coverage, test coverage, process orchestration

readme

Whiskey

Whiskey is a powerful test runner for Node.js applications and a process orchestration framework which makes running integration tests with a lot of service / process dependencies easier.

Features

  • Each test file runs isolated in a separate process
  • Support for running multiple tests in parallel in a single suite (--concurrency option)
  • Support for running multiple suites in parallel (--independent-tests option)
  • Support for a test initialization function which is run before running the tests in a test file
  • Support for a test file timeout
  • Per-test setUp / tearDown function support
  • Per-suite (test file) initialize / finalize function support
  • Per-session, or global, setUp / tearDown function support
  • Support for different test reporters (cli, tap)
  • Support for code coverage (cli reporter, html reporter)
  • Support for reporting variables which have leaked into a global scope
  • Nicely formatted reports (colors!)
  • Integration with node debugger
  • Support for generating Makefiles with different Whiskey targets

Changes

For changes please see CHANGES.md file.

Installation

Install it using npm:

npm install whiskey

Usage

whiskey [options] --tests "<test files>"

whiskey [options] --independent-tests "<test files>"

whiskey [options] --tests "<test files>"  --independent-tests "<test files>"

Available options

  • -t, --tests - Whitespace separated list of test suites to run sequentially
  • -T, --independent-tests - Whitespace separated list of test suites to run concurrently
  • -m, --max-suites NUMBER - The number of concurrently executing independent test suites (defaults to 5)
  • -ti, --test-init-file - A path to the initialization file which must export init function and it is called in a child process *before running the tests in each test file
  • -c, --chdir - An optional path to which the child process will chdir to before running the tests
  • -g, --global-setup-teardown STRING - Specifies the file containing the globalSetUp and globalTearDown procedures.
  • --timeout [NUMBER] - How long to wait for tests to complete before timing out
  • --failfast - Stop running the tests on a first failure or a timeout
  • --no-styles - Don't use styles and colors
  • --concurrency [NUMBER] - Maximum number of tests which will run in parallel (defaults to 1)
  • --quiet - Don't print stdout and stderr
  • --real-time - Print stdout and stderr as soon as it comes in
  • --test-reporter [cli,tap] - Which test reporter to use (defaults to cli)
  • --coverage - Use this option to enable the test coverage
  • --coverage-reporter [cli,html] - Which coverage reporter to use (defaults to cli)
  • --coverage-dir - Directory where the coverage HTML report is saved
  • --scope-leaks - Record which variables were leaked into a global scope
  • --scope-leaks-reporter [cli] - Which scope leak reporter to use (defaults to cli)
  • --debug NUMBER - Attach a debugger to a test process listening on the specified port number
  • --report-timing - Report each test run time
  • --dependencies STRING - Specify path to the dependencies file for the process runner. More information about the process runner can be found at PROCESS_RUNNER.md
  • --only-essential-dependencies - Only start dependencies required by the tests files which are ran. This option is only applicable if --dependencies option is used.

Note: When specifying multiple test a list with the test paths must be quoted, for example: whiskey --tests "tests/a.js tests/b.js tests/c.js"

A Note about setUp and tearDown

Presently, two kinds of setup and teardown procedures exist with Whiskey. setUp and tearDown work on a per-test basis; that is, Whiskey invokes setUp before running a test in a given Javascript file, called a suite and tearDown is invoked after a test run has finished. If you run multiple suites in parallel (e.g., via the -T/--independent-tests option), you'll get concurrent execution of setups and teardowns as well.

Sometimes, though, you need longer-lived environmental configurations, or you need safe resource sharing between entire batches of independently running tests. For these, you'll want to use globalSetUp and globalTearDown.

  • When do I use setUp / tearDown?
    • When a suite's runtime environment does not influence other running suites.
  • When do I use globalSetUp / globalTearDown ?
    • When a suite's runtime environment can potentially interfere with other, concurrently running suites.
    • Example: Attempting to run multiple suites in parallel which rely on a Cassandra schema being in place, and each attempting to reset the schema to a known state on a single Cassandra instance, you'll get Cassandra schema version errors. Using globalSetUp prevents this by running the schema reset code exactly once for all tests.

Test File Examples

A simple example (success):

var called = 0;

exports.test_async_one_equals_one = function(test, assert) {
  setTimeout(function() {
    assert.equal(1, 1);
    called++;
    test.finish();
  }, 1000);
};

exports.tearDown = function(test, assert) {
  assert.equal(called, 1);
  test.finish();
};

A simple example (skipping a test):

var dbUp = false;

exports.test_query = function(test, assert) {
  if (!dbUp) {
    test.skip('Database is not up, skipping...');
    return;
  }

  assert.equal(2, 1);
  test.finish();
};

A simple example (failure):

exports.test_two_equals_one = function(test, assert) {
  assert.equal(2, 1);
  test.finish();
};

A simple example using the optional BDD module:

var bdd = require('whiskey').bdd.init(exports);
var describe = bdd.describe;

describe('the bdd module', function(it) {
  it('supports it(), expect(), and toEqual()', function(expect) {
    expect(true).toEqual(true);
  });
});

A simple example demonstrating how to use global setup and teardown functionality:

exports['globalSetUp'] = function(test, assert) {
  // Set up database schema here...
  // Push known data set to database here...
  test.finish();
}

exports['globalTearDown'] = function(test, assert) {
  // Drop database here...
  test.finish();
}

For more examples please check the example/ folder, and the test/run.sh script.

Build status

Build Status

Running Whiskey test suite

To run the Whiskey test suite, run the following command in the repository root directory.

npm test

If all the tests have sucessfully passed, the process should exit with a zero status code and you should see * * * Whiskey test suite PASSED. * * * message.

Contributing

To contribute, fork the repository, create a branch with your changes and open a pull request.

Debugging

If you want to debug your test, you can use the --debug option. This will cause Whiskey to start the test process with the V8 debugger functionality. You then need to manually connect to the debugger to control it (i.e. using node repl or node-inspector).

Whiskey will also by default set a breakpoint at the beginning of your test file.

Note: This option can only be used with a single test file. Further, you cannot use the --debug and --independent-tests options together. The semantics just don't make any sense. To debug a test, make sure you invoke it with --tests instead.

Troubleshooting

I use long-stack-straces module in my own code and all of the tests get reported as succeeded

Long stack traces modules intercepts the default Error object and throws a custom one. The problem with this is that Whiskey internally relies on attaching the test name to the Error object so it can figure out to which test the exception belongs. long-stack-traces throws a custom Error object and as a consequence test name attribute gets lost so Whiskey thinks your test didn't throw any exceptions.

The solution for this problem is to disable long-stack-trace module when running the tests. This shouldn't be a big deal, because Whiskey internally already uses long-stack-traces module which means that you will still get long stack traces in the exceptions which were thrown in your tests.

My test gets reported as "timeout" instead of "failure"

If your test gets reported as "timeout" instead of "failure" your test code most likely looks similar to the one below:

exports.test_failure = function(test, assert){
  setTimeout(function() {
    throw "blaaaaah";
    test.finish();
  },200);
};

The problem with this is that if you run tests in parallel (--concurrency > 1) and you don't use a custom assert object which gets passed to each test function, Whiskey can't figure out to which test the exception belongs. As a consequence, the test is reported as "timed out" and the exception is reported as "uncaught".

The solution for this problem is to run the tests in sequential mode (drop the --concurrency option).

License

Apache 2.0, for more info see LICENSE.

changelog

Changes

0.8.4 (13.08.2013)

  • Fix global setup and teardown functionality so it works correctly if those procedures or the actual tests are asynchronous.

  • Update instanbul code coverage integration to use --complete-copy option.

0.8.3 (05.06.2013)

  • If user passes in --chdir option, always resolve it to a full absolute path.

  • Modify code coverage instrumentation layer to use istanbul instead of jscoverage. #55

    [Sam Falvo]

v0.8.2 (03.06.2013)

  • Remove old and obsolete _debugger module, allow user to specify debugger listen port using --debug option and print the port to console. #54

    [Chase Douglas]

v0.8.1-alpha (20.05.2013)

  • Correctly log cmd string in the process runner.

v0.8.0-alpha (20.05.2013)

  • Make Whiskey behave more like other test runners and run setUp and tearDown function before and after every test run instead of running it once per test file.

    Note: This change is breaking and backward incompatible. If you want to preserve old behavior, you need to migrate to a new initialize and finalize function. Those two functions behave the same as way setUp and tearDown did in older versions.

Old code (pre 0.8.0):

exports['setUp'] = function(test, assert) {
  // load database fixtures
  test.finish();
};

exports['tearDown'] = function(test, assert) {
  // clear database
  test.finish();
};

New code (to preserve the old behavior, post 0.8.0):

exports['initialize'] = function(test, assert) {
  // load database fixtures
  test.finish();
};

exports['finalize'] = function(test, assert) {
  // clear database
  test.finish();
};

v0.7.1 (07.05.2013)

  • Correctly report exception if a test throws a string.

v0.7.0 (15.04.2013)

v0.6.13 (18.02.2013)

  • Change with on the SpyOn object to withArgs because with is a reserved keyword.

    [Bjorn Tipling]

v0.6.12 (18.02.2013)

  • Add support for spying on the arguments with which a function has been called.

    [Michael Bird]

v0.6.11 (11.10.2012)

  • Add independent-tests option to the runner which allows user to run multiple test files in parallel.

    [Samuel A. Falvo II]

  • Add an optional, minimal BDD idiom implementation.

    [Robert Chiniquy]

  • Send the SIGKILL signal instead of SIGTERM when killing child processes managed by the process runner.

    [Robert Chiniquy]

v0.6.10 (11.05.2012)

  • Add 'spy' functionality to the 'test' object.

    [Bjorn Tipling]

v0.6.9 (06.05.2012)

  • Allow user to specify "kill_script" attribute in the process runner dependency file. If this attribute is present, the kill_script is executed instead of sending SIGTERM to the process when stopping it.

v0.6.8 (18.04.2012)

  • Allow user to pass a comma delimited string with config paths to the whiskey-process-runner binary. For example: whiskey-process-runner --config tests/dependencies1.json,tests/dependencies2.json. In case multiple paths are provided, Whiskey performs simple merge on all the values.

v0.6.7 (09.02.2012)

  • Add --coverage-no-regen option. If this option is used coverage won't be regenerated if a lib-cov directory already exists in a current working directory.

v0.6.6 (26.01.2012)

  • Add --coverage-no-instrument option
  • Also support files with .java suffix when aggregating coverage. Contributed by Gary Dusbabek.

v0.6.5 (08.01.2012)

  • Modify JSON coverage reporter so it can also be used for aggregated coverage output.

v0.6.4 (07.01.2012)

  • Add and export installCoverageHandler function.

  • Add 'available_for_coverage' option to the process runner. If a process specifies this option it is sent SIGUSR2 signal when stopping it instead of sending SIGTERM.

  • Allow user to specify full path when using a JSON coverage reporter.

v0.6.4 (16.12.2011)

  • Fix a race condition when using code coverage and instrumenting the code.

v0.6.2 (01.12.2011)

  • If an error occurs when the process runner is starting a processes, propagate it to the ProcessRunner.start callback. Patch by Ken Wronkiewicz.

v0.6.1 (29.11.2011)

  • Make sure to kill the processes which have already been started by the process runner if starting one of the processes fails. Reported by Ryan Phillips.

  • If process runner is used with Whiskey, make sure to stop all the running processes if Whiskey errors out. Reported by Ryan Phillips.

  • Log an error if test.finish() has been called more then once.

0.6.0 (27.11.2011)

  • Add process runner and support for managing and orchestrating test dependencies. More info about the process runner can be found at PROCESS_RUNNER.md.

0.5.1 (06.11.2011)

  • Fix a bug which caused an infinite loop in the CLI reporter if a test name was too long.
  • If --report-timing option is used also print aggregated run time for each test file.

v0.5.0 (05.11.2011)

  • Add --report-timing option which reports each test run time.
  • If an error object has no message attribute, but it has toString method, call this method and assign a returned value to the message attribute
  • Add --gen-makefile and --makefile-path option which allows users to generate a Makefile with different Whiskey targets
  • Remove all the code which modifies require.paths so now Whiskey also works with node v0.5.x / v0.6.x.

    Note: Now when using code coverage you must manually set NODE_PATH environment variable and make sure it contains the lib-cov directory.

v0.4.2 (29.08.2011)

  • assert.ifError now also captures a stack trace
  • Don't set a first breakpoint at the beginning of the test file when using --debug option
  • User can now pass a reason / message to the test.skip() function
  • Add a new 'json' coverage reporter which writes a raw JSON coverage dump to a file
  • Add a new --coverage-files option which allows user to generate aggregated coverage report across multiple files.

v0.4.1 (11.07.2011)

  • Fix a bug with reporting coverage when multiple test files had the same name
  • Allow user to specify which tests in a test file are run using a glob pattern

v0.4.0 (15.06.2011)

  • Add experimental support for attaching Node debugger to the test process (--debug option)
  • Fix a bug in assert.response
  • Fix a bug with exiting prematurely in the option parser on Mac OS X
  • Default Whiskey communication socket path now contains a random component
  • --print-stdout and --print-stderr options have been replaced with the --quiet option
  • The tests now run in sequential mode by default (old behavior can be replicated by using the --concurrency 100 option)
  • Fix a bug in scope leak reporting
  • Fix a bug in assert.eql

v0.3.4 (31.05.2011)

  • When reporting the test results print a whole path to the test file instead of just a file name
  • Add --no-styles option and only use styles and colors if the underlying terminal supports it
  • Don't patch EventEmitter.on and EventEmitter.addListener in the long-stack-traces library, because this masks original functions and breaks some functionality
  • Add support for skipping a test using test.skip() function
  • Allow user to directly pass in a list of test to run to the whiskey binary without using the --tests option

v0.3.3 (17.05.2011)

  • Make test object a function and allow users to directly call this function to signal end of the test [Wade Simmons]
  • Add support for scope leaks reporting (--scope-leaks option)

v0.3.2 (04.05.2011)

  • Allow user to pass in --encoding and --exclude option to jscoverage
  • When a test file times out, print the results for all the tests in this file which didn't time out
  • Refactor some of the internals so the results are now reported back to the main process after each test completes instead of reporting them back when all the tests in a single file complete
  • Clear the timeout and report the tests result in the child exit handler and not after all the tests have called .finish(), because it's possible that user calls .finish() and blocks afterwards

v0.3.1 (02.05.2011)

  • Capture the child process stdout and stderr in the main process instead of monkey patching the process.stdout and process.stderr in the child process

v0.3.0 (01.05.2011)

  • Refactor most of the internals to make the code more readable and more easy to extend
  • Communication between the main and child processes now takes place over a unix socket
  • Add support for "Reporter" classes
  • Remove the --init-file option
  • User can now specify a maximum number of async tests which will run at the same time (--concurrency [NUMBER] option)
  • Add a new "TAP" test reporter class (--test-reporter tap)
  • Add test coverage support with support for text and HTML output (--coverage option)
  • User can now specify a module with custom assertion functions which are attached to the assert object and passed to the each test function (--custom-assert-module MODULE_PATH)

Note: The test format has changed and it is not backward compatible with Whiskey 0.2.0.

Now each test gets passed in a special test object and a custom assert module which must be used to perform assertions.

exports['test_some_func'] = function(test, assert) {
...

v0.2.4 (15.04.2011)

  • Better reporting on a test file timeout
  • Properly report if a test file does not exist or some other uncaught exception was thrown
  • Fix a bug with output buffer not being fully flushed on exit on OS X
  • add --print-stdout and --print-stderr option

v0.2.2 (30.03.2011)

  • Add timeout support to the init, setUp and tearDown function - if the callback passed to one of those functions is not called in timeout milliseconds, an exception is thrown and test execution is aborted
  • Test timeout is now properly reported as a failure

0.2.1 (27.03.2011)

  • Handle uncaughtExceptions better
  • Use lighter colors so test status output is more distinguishable
  • Fix bug with "cannot find module" exception not being properly reported
  • Add support for per-test file init function / file
  • Print stdout and stderr on failure

v0.2.0 (26.03.2011)

  • Add support for the failfast mode (runner exists after a first failure)
  • User can specify custom test timeout by passing in the --timeout argument
  • Add support for a setUp and tearDown function
  • Add colors to the output
  • Now each test file must export all the test functions so the runner can iterate over them
  • Add support for a global initialization file / function (init function in this file is run before all the tests in a main process and can perform some kind of global initialization)
  • Add support for --chdir argument

v0.1.0 (25.03.2011)

  • Initial release (refactor module out from Cast and move it into a separate project)