Canary
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 libraryconst canary = ;// Node's built-in assertion libraryconst assert = ;// A function that ought to be tested{let text = Stringvalue;whiletextlength < lengthtext = " " + text;return text;}// Tests written using Canarycanary;
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;
And this would output to the console...
sophie:canary pineapple$ node testLeftPad.jsRunning 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 length4 of 4 tests passed.Status: OK