/riotjs

JavaScript unit tests.

Primary LanguageJavaScript

This is a JavaScript implementation of Riot.

It will run in a browser, Rhino, or Node.

Example

Tests look like this:

Riot.run(function() {
  context('basic riot functionality', function() {
    given('some simple equality tests', function() {
      asserts('a simple truth test should return true', true).isTrue();
      asserts('isNull is null', null).isNull();
    });

    given('another context', function() {
      asserts('equals should compare strings as expected', 'test string').equals('test string');
    });

    given('a context concerned with functions', function() {
      asserts('asserts() should allow functions to be compared', function() {
        return 'test string';
      }).equals('test string');
    });
  });

  given('yet another context', function() {
    asserts('equals should compare strings as expected', 'test string').equals('test string');
  });
});

There are a few aliases to make tests (and output) read more naturally:

  • context: given — Given will add “Given…” to the title in the test output
  • asserts: should

Assertions

  • equals – for example, asserts('description').equals('value')
  • matches – matches a regex
  • typeOf – aliased to isTypeOf, kindOf
  • isNull
  • isTrue
  • raises

Riot.run

Riot.run(function() { // your tests }); just adds your tests to window.onload. If there’s already an onload handler it won’t add itself. If there’s no window it will just run the tests.

Riot.load() can be used to load required scripts in a browser or interpreter. Riot.require() only loads files once by keeping a record of files that have already been loaded.

It can also be called with no parameters to run tests defined with Riot.context. This can be used to create a set of files with tests inside a Riot.context for each file

Packaging

Packaged as a RubyGem usable via XPCOMCore-RubyGemsriotjs-xpcc.

Writing a test for a browser AND interpreter

My current pattern looks like this:

var Riot;
if (typeof load !== 'undefined') {
  load('riot.js');
} else if (typeof require !== 'undefined') {
  Riot = require('./riot').Riot;
}

Riot.require('../turing.core.js');
Riot.require('../turing.oo.js');
Riot.require('fixtures/example_classes.js');  

Riot.context('turing.oo.js', function() {
  // Tests go here
});

Riot.run();
<!DOCTYPE html>
<html>
  <head>
    <title>Riot</title>
    <style>
p.pass { color: green; font-weight: bold; margin: 0 }
p.fail { color: red; font-weight: bold; margin: 0 }
#test-results ul { margin-bottom: 1em }
    </style>
    <script type="text/javascript" src="riot.js"></script>
    <script type="text/javascript" src="core_test.js"></script>
    <script type="text/javascript" src="oo_test.js"></script>
  </head>
  <body>
    <div id="test-results"></div>
  </body>
</html>

Note:

  • load() is no-op’d in browsers if it doesn’t exist by Riot
  • Riot.run() will only run once, so you can include multiple Riot test files
  • Specifying Riot.run() at the end of each test file lets you run that file with js Riot.run() in the console

Contributors

  • ac94 (Aron Carroll)
  • bgerrissen (Ben Gerrissen)