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

Package detail

jaribu

silverbucket793GPL2.2.3

a simple, full-featured, JavaScript testing framework

javascript, testing, framework, node.js, node, io.js, browser

readme

jaribu

a JavaScript (browser & node.js) testing framework

Build Status license downloads release

Intro

Jaribu is a JavaScript testing framework that runs both in the browser and via node.js. It's meant to keep things simple, and make the barrier for writing tests as thin as possible, and to run in as many javascript environments as possible.

Features

Console-based testing : jaribu will automatically find tests in test/*-suite.js when run from console.

$ node_modules/.bin/jaribu

Browser testing : When run from the browser, you can create a simple test/index.html file like the following:

NOTE : fetch is a requirement for jaribu tests to run in the browser.

<!DOCTYPE html>
<html>
<head><title>tests</title></head>
<body>
<div id="jaribuTestOutput"></div>
<script>
var jaribuTestFiles = [
    'test1-suite.js',
    'test2-suite.js', 
    'test3-suite.js' 
    ];
</script>
<script data-main="../node_modules/jaribu/browser/main.js" src="../node_modules/jaribu/node_modules/requirejs/require.js"></script>
</body>
</html>

For tests which are designed to only run on the server, you can add a runInBrowser: false boolean to the suite properties.

Shared environments : a suite has an 'env' object which you can write to and that data will be available for any test in that suite.

  suites.push({
    name: "test suite",
    desc: "example",
    setup: function(env) {
      env.foo = 'bar';
    },
    tests: [
      {
        desc: "we should have the foo property",
        run: function(env, test) {
          test.assert(env.foo, 'bar');  // true
        }
      },
      {
        desc: "lets set a var",
        run: function(env, test) {
          env.pizza = 'slice';
          test.assert(env.pizza, 'slice');  // true
        }
      },
      {
        desc: "verify it's still there",
        run: function(env, test) {
          test.assert(env.pizza, 'slice');   // true
        }
      },
      {
        desc: "remove a variable",
        run: function(env, test) {
          delete env.foo;
          test.assertType(env.foo, 'undefined');   // true
        }
      },
      {
        desc: "we shouldn't be able to access the deleted property",
        willFail: true,
        run: function(env, test) {
          test.assert(env.foo, 'bar');   // false
        }
      }
    ]
  });

Output

Generally speaking, when the tests are passing as expected, the output will be minimal. The description of each suite of tests will be displayed, followed by a series of + and !+ characters, and any errors if they occur.

  • + means the test passed.

  • !+ means the test failed, but this was expected (treated as a pass).

At the end of all test running, there will be a summary describing the total number of tests (and meta-tests, known as scaffolding) run, failures, passes, etc.

API

Asserts

You can use the assert family of functions to compare values with each other (objects, arrays, strings, types).

assert()

The assert() function compares two objects for truthiness and passes or fails the test based on the result of the comparison.

  assert(object1, object2, "testing object1 and 2 are the same")

assertAnd()

Same as assert() except does not pass the test automatically when the result is true. If the objects do not match, however, the test will fail.

assertFail()

Behaves the opposite of assert(), test will pass if the objects do not match.

assertFailAnd()

Behaves the opposite of assertAnd(), test will not fail if objects do not match, and will fail automatically if objects match.

assertType()

The assertType() function tests the type of a given variable (object, string, boolean, array, etc.). NOTE: can use 'array' as a type.

  assertType(object, 'object', "testing object is actually an object")

assertTypeAnd()

Same as assertType() except does not pass the test automatically when the result is true. If the object type is incorrect, however, the test will fail.

assertTypeFail()

Behaves the opposite of assertType(), test will succeed if the type of object is incorrect, and will automatically fail if the types match.

assertTypeFailAnd()

Behaves the opposite of assertTypeAnd(), test will not fail if the type of object is incorrect, and will automatically fail if the types match.

Mocks and Stubs

Technically they are all mocks, since they have info about whether they've been called, and how many times, but can be used as stubs as well (which are basically just mocks without meta data).

  var mock = new test.Stub(function(p1, p2) {
    console.log('hello world');
  });

  mock.called;  // false

  mock.numCalled;  // 0

  mock();  // hello world

  mock.called;  // true

  mock.numCalled;  // 1

Testing for thrown exceptions

Catching thrown exceptions works with normal thrown exceptions or exceptions thrown asyncronously. The interface is the same either way, just call the function you want to test. If it throws an exception, the test passes.

  test.throws(function () {
    throw new Error('oops');
  }, Error, 'caught thrown exception');

Shortcuts

When resolving tests there are a number of calls you can make.

  test.result(false, 'this broke because ...'); // fails test with message
  test.result(true); // passes test
  test.done(); // passes test
  test.fail('problem with stuff ...'); // fails test with message

changelog

CHANGELOG

jaribu v2.2.1 - 2015/12/17

  • added runInConsole optional boolean property to suite object. If false it will be skipped when running tests from the console.

jaribu v2.1.3 - 2015/12/16

  • another subtle require.js fix so that it can always successfully include something even if it's overridden with window.fetch right after.

jaribu v2.1.2 - 2015/12/16

  • the new node_modules layout causes issue trying to include whatwg-fetch, so it's been removed and we just say fetch must be implemented in the browser.

jaribu v2.1.1 - 2015/12/16

  • abortOnFail was not stopping all tests in the browser due to the lack of process.exit, fixed this so the behavior is the same as it is on the console.

jaribu v2.1.0 - 2015/12/03

  • re-enable test.write function, which had been disabled for the 2.0 release and never re-enabled.

jaribu v2.0.0 - 2015/05/04

  • greatly minimized text output during testrunning to a + for pass, !+ for failed but expected (aka. pass) and only displaying verbose data when there's a failure.

jaribu v1.1.0 - 2015/03/30

  • running jaribu tests in browser now as well (aside from websocket and rest tests which need server functionality)

  • added display.write for browser as well as console

  • exception handling for test.throws() method extended to work in browser

  • more Promise object bugfixes

  • more bugfixes related to dynamic loading of dependencies using aliases

  • renamed test.tools.* to test.helpers (breaking change)

jaribu v1.0.1 - 2015/03/30

  • Promise object bugfixes

jaribu v1.0.0 - 2015/03/30

  • switching to v1.0.0 and following semver

  • reworked module loadng to use AMD aliases rather than paths, so the locations can be defined at runtime

  • added display functions for use in a browser environment

  • created fetch.json test tool test.fetch.json which operates like test.helpers.fetch except has promise handling and json parsing built in

  • created browser/main.js for browser-based tests to include and bootstrap jaribu in the browser. Only the following HTML is needed in the projects test/ dir:

<!DOCTYPE html>
<html>
<head><title>tests</title></head>
<body>
<div id="jaribuTestOutput"></div>
<script>
var jaribuTestFiles = [
    'test1-suite.js',
    'test2-suite.js', 
    'test3-suite.js' 
    ];
</script>
<script data-main="../node_modules/jaribu/browser/main.js" src="../node_modules/jaribu/node_modules/requirejs/require.js"></script>
</body>
</html>

jaribu v0.4.0 - 2015/03/29

  • completely removed dependency on jquery (removed from test.tools.jquery) (breaking change)

  • removed test.http helper in favor of test.tools.fetch (breaking change)

  • updated dependencies

jaribu v0.3.0 - 2014/12/27

  • added support for using type 'array' within assertType

jaribu v0.2.2 - 2014/10/14

  • another version bump to get around broken dependency packages

jaribu v0.2.1 - 2014/10/13

  • stricter npm dependency versions, fix for a broken npm package

jaribu v0.2.0 - 2014/09/26

  • added test.fail() function to easily pass promise errors directly to jaribu.

    myLib.myMethod(function(yes) {

      test.assert(yes, true);

    }, test.fail).catch(test.fail);

  • lots of code cleanup and improvements to readability.

jaribu v0.1.10 - 2014/05/21

  • fix deep object assertion making sure we only fail if one objects property returns true for hasOwnProperty() while it returns fails for the other object

jaribu v0.1.9 - 2014/03/31

  • fix in object assertion, an object with no properties was asserting as the same as an object with an undefined property. issue #26

jaribu v0.1.8 - 2014/03/15

  • fixed informational messages displayed during test.assertFail() and test.assertFailAnd() failures.

jaribu v0.1.6 - 2014/03/04

  • added http DEL and PUT functionlity to Http object.

jaribu v0.1.4 - 2014/03/03

  • renamed project to 'jaribu'.

  • initial work on using tests within a browser environment (thanks @ggrin)

  • better array comparisons during assert (thanks @michielbdejong)

  • added some http test as examples.

jaribu v0.1.2 - 2013/12/21

  • improved stack trace reporting during failures.

jaribu v0.1.1 - 2013/12/18

  • fixed assert* informational messages. In some cases the automatic messages which indicated the location where the object match failed was being set as undefined.

jaribu v0.1.0 - 2013/12/10

  • bugfix for custom error messages during assert*()'s. Previously it wasn only working for assertAnd(). (issue #14)

  • testing tools (functions) which previously needed the test object passed in, don't anymore.

  • added support for testing for thrown exceptions. (feature request #15)

      this.throws(function () {
          throw new Error('oops');
      }, Error, 'caught thrown exception');
  • fixed an issue where the maximum call stack size was being exceeded when running thousands of tests.

  • refactoring and improvement of code clarity (using codeclimate.com as a benchmark)

jaribu v0.0.19 - 2013/11/15

  • refactored the jaribu.loadSuite() function to reduce function complexity, splitting out class functions to separate files: lib/Scaffolding.js, lib/Teste.js, lib/Suite.js, lib/helpers.js. (https://codeclimate.com/github/silverbucket/jaribu).

  • minor adjustments to logging behavior.

  • if a jaribu returns a promise with a fail function, we can use that to catch unexpected errors.

jaribu v0.0.18 - 2013/07/10

  • Fixes to WebSocketClient tool

jaribu v0.0.17 - 2013/06/23

  • added abortOnFail boolean to suite options. If a test fails in that suite, entire execution is halted. This is useful for cases where you know everything is going to break if any tests in a suite fail.

    suites.push({
      desc: "checks for various version requirements",
      abortOnFail: true,  // don't continue with further test suites if any
                          // tests in this suite fail
      setup: function (env, test) {
          ...
      },
      tests: [{...}]
    });

jaribu v0.0.16 - 2013/03/20

  • minor bugfixes for assert and WebSocketClient

jaribu v0.0.15 - 2013/03/09

  • decreased wait interval so async tests complete faster.

  • added done() as an alias to result() for tests.

  • minor fixes, binded functions so they can be used as callbacks.

jaribu v0.0.14 - 2013/02/14

  • bugfixes in the assert and WebSocketClient tools.

jaribu v0.0.13 - 2013/01/30

  • created a new function for the WebSocketClient, called sendWith(). it's meant to replace all of the functionality of both sendAndVerify() and sendWithCallback(), using a single properties object (param object), this we the function can be extended, and modified in the future without worrying about param order, instead sending a single object with named properties.

    A list of all available properties at this time:

      // {
      //   send: JSON.stringify(data),
      //   expect: expected,
      //   confirmProps: confirmProps,
      //   autoVerify: true,
      //   onComplete: function() { }, // if callback function is called,
      //                                                             // verification is used with assertAnd,
      //                               // not assert.
      //
      //   onMessage: function() { },  // mutually exclusive to autoVerify, if
      //                               // autoVerify is set, this is not called
      //
      //   onError: function() { },  // mututall exclusive to autoVerify
      //                             // autoVerify is set, this is not called
      // }

jaribu v0.0.12 - 2013/01/28

  • added support for confirmation messages in WebSocketClient.sendAndVerify() function. this allows you to say you are expecting a confirmation message before the actual result you eventually want to test.

      var confirmProps = {
          status: true,
          verb: 'confirm'
      };
      var data = {
          platform: "dispatcher",
          object: {
              secret: '1234567890'
          },
          verb: "register",
          rid: "123454"
      };
      var expected = {
          status: true,
          rid: "123454",
          verb: 'register',
          platform: "dispatcher"
      };
      env.connection.sendAndVerify(JSON.stringify(data), expected, test, confirmProps);

    Params are: data to send, expected result data, test object, confirm properties.

jaribu v0.0.11 - 2013/01/17

  • modified WebSocketClient's sendAndVerify() function. Now it takes three params: send data, expected data, and test object. and you no long pass 'messages' data to client.

      setup: function(env, test) {
          env.expected = { // struct of expected results for each http call
              setupTest: 'setupTest',
              test: {
                  foo: "bar"
              },
              footwear: {
                  leather: "boots",
                  flip: "flops",
                  block: "of wood"
              }
          };
    
          var client = new this.WebSocketClient({
              url: 'ws://localhost:9992/',
          });
    
          client.connect(function(connection) {
              env.connection = connection;
              env.connection.sendAndVerify('setupTest', env.expected.setupTest, test);
          });
      },
      tests: [
          {
              desc: 'auto validate websocket command',
              run: function (env, test) {
                  env.connection.sendAndVerify('footwear', env.expected.footwear, this); // passes
              }
          },
          {
              desc: 'the first level of properties are the commands',
              run: function (env, test) {
              env.connection.sendAndVerify('blah', 'lalala', test); // fails
              }
          }
      ]

jaribu v0.0.10 - 2013/01/15

  • support for promises in tests:

      {
          desc: "async call making use of promises",
          run: func(env, test) {
              return someAsyncCall(function(result) {
                  test.assert(result, 'success');
              });
          }
      }
  • failing tests now get a generic stack trace to aid in debugging.

  • added second parameter to all tests, the 'test' object will help in cases where you constantly have to re-assign 'this' due to async callbacks.

      {
          desc: 'test with this',
          run: func(env) {
              this.result(true);
          }
      },
      {
          desc: 'test with test param',
          run: func(env, test) {
              someAsyncFunction(function(status) {
                  test.result(status);
              });
          }
      }
  • assert*() functions now take an optional 3rd parameter which is printed along with the error message when the assert fails, for more informative information.

  • bugfixes where assert*And() fails were slipping through the cracks

jaribu v0.0.9 - 2012/11/25

  • added support for performing tests against WebSocket servers. If you provide the WebSocketClient with the expected data result for each command you can use auto verification to easily test the responses.

      setup: function(env) {
          env.expected = { // struct of expected results for each http call
              setupTest: 'setupTest',
              test: {
                  foo: "bar"
              },
              footwear: {
                  leather: "boots",
                  flip: "flops",
                  block: "of wood"
              }
          };
    
          var client = new this.WebSocketClient({
              url: 'ws://localhost:9992/',
              messages: env.expected  // data struct of commands and expected
                                          // results
          });
    
          var _this = this;
          client.connect(function(connection) {
              env.connection = connection;
              env.connection.sendAndVerify('setupTest', _this);
          });
      },
      tests: [
          {
              desc: 'auto validate websocket command',
              run: function(env) {
                  env.connection.sendAndVerify('footwear', this); // passes
              }
          },
          {
              desc: 'the first level of properties are the commands',
              run: function(env) {
              env.connection.sendAndVerify('blah', this); // fails
              }
          }
      ]

jaribu v0.0.8 - 2012/11/08

  • added support for HTTP GET / POST testing, using a simple jQuery wrapper. All you can do with jQuery.ajax() applies. But for simple cases:

      {
          desc: 'http get example',
          run: function(env) {
              var _this = this;
              env.http = new this.Http()
              env.http.get('/', {
                  success: function(data, textStatus, jqXHR) {
                      _this.assert(data, <expected_data>);
                  },
                  error: function() {
                      _this.result(false, 'failed');
                  }
              });
          }
      },
      {
          desc: 'http post example',
          run: function(env) {
              var _this = this;
              env.http.post('/', {foo:'bar'}, {
                  success: function(data, textStatus, jqXHR) {
                      _this.assert(data, <expected_data>);
                  },
                  error: function() {
                      _this.result(false, 'failed');
                  }
              });
          }
      }
  • added a simple JSON HTTP server. It takes a data struct as an argument and uses it to make URIs/results.

      setup: function(env) {
          var data = { // struct of expected results for each http call.
              test: { // the first set of properties are URIs, all children
                      // of these properties are the data returned.
                  foo: "bar"
              }
          };
          var server = new this.HttpServer({
              port: 9991,
              uris: data
          });
          var _this = this;
          server.run(function() {
              _this.write('http dummy server running');
    
              var http = new _this.Http({
                  baseUrl: 'http://localhost:9991'
              });
    
              env.http.get('/test', {
                  success: function(data, textStatus, jqXHR) {
                      _this.assert(data, {foo:'bar'});
                  },
                  error: function() {
                      _this.result(false, 'failed http request on /');
                  }
              });
          });
      }
  • added stack traces for failures

jaribu v0.0.7 - 2012/11/06

  • added support for specifying suite files to run via. the command-line (@nilclass)

  • switched to using requirejs by default for all file inclusions. Suite files now begin like this:

      if (typeof define !== 'function') {
          var define = require('amdefine')(module);
      }
      define(['requirejs'], function(requirejs, undefined) {
          var suites = [];
          // ... tests
           return suites;
      });

jaribu v0.0.6 - 2012/10/28

  • encase test and scaffolding runs in a try/catch clause

  • now use the term 'willFail' for announcing ahead of time that a test with fail.

      {
          desc: "this test will fail, and that should pass",
          willFail: true,
          run: function(env) {
              this.result(false);
          }
      }
  • display output now handled by the display.js library, which currently just supports console output, but can be extended to support HTML output (once we get in-browser testing working).

  • added test functions this.assertFail(), this.assertFailAnd(), this.assertTypeFail(), this.assertTypeFailAnd().

    {
        desc: "if an assertFail fails, that resolves to a passed test",
        run: function(env) {
            this.assertFailAnd(true, false);
            this.assertFail('blah', 'bad');
        }
    }