jstd-mocha

Run JsTestDriver tests on Mocha.

Usage no npm install needed!

<script type="module">
  import jstdMocha from 'https://cdn.skypack.dev/jstd-mocha';
</script>

README

jstd-mocha

Run your JsTestDriver tests directly within Mocha. This project borrows source code from karma-jstd.

Build Status

Usage

After installing with npm install --save-dev jstd-mocha you can run JsTestDriver tests like this:

require('jstd-mocha').installTo(global);

var MyTestCase = TestCase('JsTestDriver (first suite)');

MyTestCase.prototype.setUp = function() {
    this.x = true;
};

MyTestCase.prototype.testThatXIsTrue = function() {
    assertTrue('x should have been set to true in setUp()', this.x);
};

Running The tests

Because JsTestDriver tests are added to the test-case after it's created, jstd-mocha needs to be nudged once all of the tests have been registered, so you can't just run mocha test as you normally might — assuming your tests are all inside a test directory.

Instead, you can either use the --delay flag, as follows:

mocha test --delay

or create a jstd-register.js file in your own project having these contents:

require('jstd-mocha/register');

and then run the tests like this:

mocha test jstd-register

This second way of doing things is particularly useful if you are planning to run your Mocha tests in a browser using the karma-mocha plug-in. Take a look at this project's package.json and karma.conf.js for examples of this.

Migrating to Mocha

Since jstd-mocha is mostly just a thin wrapper around Mocha and expectations, it isn't too hard to permanently change your tests so they run directly in Mocha. Additionally, since Mocha has an identical API to Jasmine for everything but asynchronous tests, and since expectations is a port of the Jasmine assertion API, tests should also be runnable in Jasmine too.

There are some caveats however. The more complex assertions (e.g. assertEquals()) aren't completely compatible with their Jasmine counterparts (e.g. .toEqual()), so we instead map these through to the assertion library that ships with js-test-driver by default.

However, you can migrate your tests over to using 100% Jasmine compatible expectations by using these assertions instead:

  • $assertEquals() instead of assertEquals() (uses the same definition of object equality as .toEqual()).
  • $assertException() instead of assertException() (if an argument is provided, expects that argument to be equal to the error that will be thrown, rather than expecting a string containing only the type of the error that will be thrown).

Once you've done this for all tests, the final conversion can be done completely mechanically.