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

Package detail

canary-test

Mapita114MIT1.1.2TypeScript support: included

Write and run automated tests in JavaScript code.

canary, test, testing, unit test, automated test, stability

readme

Canary

Build Status Documentation Status

Canary is a tool for writing and running automated tests in JavaScript code. It was written with tests and implementation sharing the same file foremost in mind. When tests and implementation are close together, it's more difficult to forget or ignore tests when making changes to the implementation.

Check out Canary's documentation at canary.readthedocs.io.

Install Canary using npm: npm install canary-test

Canary lets you control where and how you write your tests, how they are run, and how their results are reported. In case you aren't worried about the specifics of your how your test runner functions and reports results, just as long as it works, Canary can also run tests and report results with one function call for sensible default behavior.

Canary works best when used in combination with an assertion library such as chai or node's assert module.

Here's a simple example of JavaScript code tested using Canary:

// This library
const canary = require("canary-test").Group("leftPad package");
// Node's built-in assertion library
const assert = require("assert");

// A function that ought to be tested
function leftPad(value, length){
    let text = String(value);
    while(text.length < length){
        text = " " + text;
    }
    return text;
}

// Tests written using Canary
canary.group("leftPad", function(){
    this.test("returns the input when it's as long as or longer than the input length", () => {
        assert.equal(leftPad("hello", 3), "hello");
        assert.equal(leftPad("world", 5), "world");
    });
    this.test("pads shorter inputs with spaces to match the desired length", () => {
        assert.equal(leftPad("hi", 4), "  hi");
        assert.equal(leftPad("123", 6), "   123");
    });
});

These tests could then be run with a single function call. This call might be placed in its own JavaScript file that runs when when you npm run test, or it might be placed in your main application code behind a command-line arugment switch, or any way that works best for you.

canary.doReport();

And this would output to the console...

sophie:canary pineapple$ node testLeftPad.js
Running tests via Canary...
Completed test "leftPad => returns the input when it's as long as or longer than the input length". (0.000s)
Completed test "leftPad => pads shorter inputs with spaces to match the desired length". (0.000s)
Completed test group "leftPad". (0.001s)
Completed test group "leftPad package". (0.002s)
Finished running 4 tests.
✓ leftPad package
  ✓ leftPad
    ✓ returns the input when it's as long as or longer than the input length
    ✓ pads shorter inputs with spaces to match the desired length
4 of 4 tests passed.
Status: OK

changelog

v1.1.2

Released 5 June 2019.

  • Report objects now include a suggested process exit status.
  • The doReport function now accepts a logFunction option.
  • The doReport function now accepts an addSections option.

v1.1.1

Released 30 January 2019.

  • Convert main test script to TypeScript.
  • Improve typings for callback functions used by tests. (Specify "this" type.)
  • The second argument to CanaryTest.addError is now optional.

v1.1.0

Released 16 January 2019.

  • Add typings for TypeScript; include definitions in the package.
  • Beware changes to API due to typing and switching to ES6 modules.

v1.0.3

Released 19 July 2018.

  • Fix issue reporting errors thrown in callbacks during doReport.

v1.0.2

Released 7 May 2018.

  • Fix handling of errors with multi-line messages
  • Fix log spam when using the paths options for doReport
  • Improve handling of groups and series without body functions
  • Improve handling of errors in skipped/filtered tests

v1.0.1

Released 13 April 2018.

  • Add canary.Group and canary.Series
  • Fix logging bug when calling doReport

v1.0.0

Released 29 March 2018.