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 = require('willy').will;
describe('some test suite', function () {
it('should do X, Y, and Z', function () {
// Will it?
will(true).be(true);
});
});
Tests
- be
- beA/beAn
- beLike
- exist
- have
- haveAny
- haveOnly
- haveOwn
- not
- throw
Utilities
- addTest
checks for identity (===)
// pass
will(3).be(3);
// fail
will('3').be(3);
checks for inheritance (instanceof) - These are synonyms.
// pass
will('').beA(String);
will([]).beAn(Array);
// fail
will('').beA(Number);
checks for equality (==)
// pass
will('').beLike(false);
// fail
will('false').beLike(false);
checks existence
// pass
var foo = { bar: 1 };
will(foo.bar).exist();
// fail
will(foo.baz).exist();
checks for items/properties in an Array/Object - All must be present.
// pass
will([1, 2, 3]).have(1);
will([1, 2, 3]).have([1, 2]);
will({ foo: 1, bar: 1 }).have('foo');
will({ foo: 1, bar: 1 }).have(['foo', 'bar']);
// fail
will([2, 3]).have(1);
will([1, 2]).have([1, 3]);
will({ foo: 1, bar: 1 }).have('baz');
will({ foo: 1, bar: 1 }).have(['foo', 'baz']);
checks for the existence of one item/property in an Array/Object
// pass
will([1, 2, 3]).haveAny(1);
will([1, 2, 3]).haveAny([3, 6]);
will({ foo: 1, bar: 1 }).haveAny('foo');
will({ foo: 1, bar: 1 }).haveAny(['foo', 'baz']);
// fail
will([2, 3]).haveAny(1);
will([1, 2]).haveAny([3, 6]);
will({ foo: 1, bar: 1 }).haveAny('baz');
will({ foo: 1, bar: 1 }).haveAny(['baz', 'quux']);
checks Array/Object for unexpected items/properties
// pass
will([1]).haveOnly(1);
will([1, 2, 3]).haveOnly([1, 2, 3]);
will({ foo: 1 }).haveOnly('foo');
will({ foo: 1, bar: 1 }).haveOnly(['foo', 'bar']);
// fail
will([1, 2]).haveOnly(1);
will([1, 2, 3]).haveOnly([1, 2]);
will({ foo: 1, bar: 1 }).haveOnly('baz');
will({ foo: 1, bar: 1, baz: 1 }).haveOnly(['foo', 'bar']);
checks for own properties (hasOwnProperty)
var Foo = function () {};
var foo = new Foo();
foo.bar = true;
Foo.prototype.baz = true;
// pass
will(foo).haveOwn('bar');
// fail
will(foo).haveOwn('baz');
negates the logic of any assertion
// pass
will(true).not.be(false);
will([1, 2]).not.haveOnly(1);
// fail
will('foo').not.be('foo');
will([1]).not.haveOnly(1);
checks for errors being thrown
var bad = function () {
throw new Error('whoops');
};
var good = function () {};
// pass
will(bad).throw();
// fail
will(good).throw();
add your own test to Willy
Add custom tests by passing a named function to willy.addTest
.
- Get the value that is being tested with
this.item
. - Test truth (with respect for the
not
prefix) withthis.isTrue
andthis.isFalse
. - Throw an error when your criteria is not met with
this.raise
.raise
takes two parameters:- a string explaining what you were testing
- the values you were testing against
var willy = require('willy'),
will = willy.will;
willy.addTest(function beLessThan(x) {
if (this.isFalse(this.item < x)) {
this.raise('be less than', x);
}
});
// passes
will(1).beLessThan(2);
// fails
will(2).beLessThan(2); // 'expected <2> to be less than <2>'
will(1).not.beLessThan(2); // 'expected <1> not to be less than <2>
56B8
;'