Installation | Built-in Tests | Custom Tests
Willy is an assertion library designed to be simple and readable. It doesn't follow the normal BDD/TDD assertion styles, but reads more like questions.
var will = will; ;
Willy...
- is super easy to use
- includes a bunch of built-in tests
- makes custom tests easy
- supports promises
Keep it simple, so you can focus on your code, not your tests. Compare testing instanceof
in a few different assertion libraries.
// Chaitobean; // Shouldjsfooshouldbean // Jasmine; // Willy;
Want to add to Willy's repertoire? That's easy, too.
willy; ;// expected 'potato' to be a substring of 'Bender Bending Rodriguez'
Installation
Use npm, man. Keep it simple.
npm install willy --save-dev
Usage
Built-in Tests
- be
- beA
- beAn
- beDefined
- beFalsy
- beGreaterThan
- beLessThan
- beLike
- beMoreThan
- beNull
- beTruthy
- beUndefined
- exist
- have
- haveAny
- haveOnly
- haveOwn
- match
- throw
be
Test identity.
// pass; // fail;
beA/beAn
Test inheritance.
This also handles some of the weirdness of JavaScript, so that String
and Number
literals act as expected.
var {};var foo = ; // pass;;; // fail;
beDefined
Test for a defined value.
var foo = 123;var bar; // pass; // fail;
beFalsy
Test for a falsy value.
// pass; // fail;
beGreaterThan
Test a value to see if it's greater than another.
// pass; // fail;
beLessThan
Test a value to see if it's less than another.
// pass; // fail;
beLike
Test equality.
// pass; // fail;
This also works on objects, including Arrays, recursively.
// pass; ; // fail; ;
beMoreThan
Test a value to see if it's more than another.
// pass; // fail;
beNull
Test for null
.
// pass; // fail;
beTruthy
Test for a truthy value.
// pass; // fail;
beUndefined
Test for an undefined
value.
var foo;var bar = 123; // pass; // fail;
exist
Test the existence of a property.
// passvar foo = bar: 1 ; ; // fail;
have
Test the existence of multiple items/properties in an Array/Object. all must be present
// pass;;;; // fail;;;;
haveAny
Test the existence of one item/property in an Array/Object
// pass;;;; // fail;;;;
haveOnly
Test an Array/Object for unexpected items/properties.
// pass;;;; // fail;;;;
haveOwn
Test for an own property.
var {};var foo = ; foobar = true;Fooprototypebaz = true; // pass; // fail;
match
Test against RegExp.
// pass; // fail;
throw
Test for an error being thrown.
var { throw 'whoops';}; var {}; // pass; // fail;
Custom Tests
To define new tests, use willy.define
. The function should return an expression used to determine if the test passes or fails.
willy; ;// expected 'potato' to be a substring of 'Bender Bending Rodriguez'
Inside every test function
this.actual
is the value passed towill()
this.expected
is the optional value passed to the test
Advanced
Pass an object to willy.define
to give you more options. The object's properties should be:
- fn - The function used to perform the test.
- explanation (optional) - An explanation of what you were testing. If omitted, the test's name will be converted.
- name (optional) - The name of your test. This can usually be figured out, but ocassionally you may want to be explicit.
Changing the Explanation
// providing a better explanationwilly; ;// expected 'potato' to be found inside of 'Bender Bending Rodriguez'
Changing the Name
// providing a different namewilly; ;// expected 'potato' to lie within 'Bender Bending Rodriguez'
Bulk Definitions
If you're lazy, use willy.loadDefinitions
to define multiple tests at a time.
willy;
Modularize It
For supreme laziness, keep your custom tests in a Node module. This will allow you to build up a collection of custom tests you can reuse with all your projects.
my-tests.js
exportsbeASubstringOf = { return thisexpected > -1; } explanation: 'be found inside of'; exportslieWithin = { return thisexpected > -1; };
A Test Suite
var willy = will = willywill; willy; ;