8000 GitHub - reergymerej/willy at v0.2.1
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

reergymerej/willy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Willy

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);

    });
});

API

Index

Tests

  • be
  • beA/beAn
  • beLike
  • exist
  • have
  • haveAny
  • haveOnly
  • haveOwn
  • not
  • throw

Utilities

  • addTest

Tests

be

checks for identity (===)

// pass
will(3).be(3);

// fail
will('3').be(3);
beA/beAn

checks for inheritance (instanceof) - These are synonyms.

// pass
will('').beA(String);
will([]).beAn(Array);

// fail
will('').beA(Number);
beLike

checks for equality (==)

// pass
will('').beLike(false);

// fail
will('false').beLike(false);
exist

checks existence

// pass
var foo = { bar: 1 };

will(foo.bar).exist();

// fail
will(foo.baz).exist();
have

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']);
haveAny

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']);
haveOnly

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']);
haveOwn

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');
not

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);
throw

checks for errors being thrown

var bad = function () {
    throw new Error('whoops');
};

var good = function () {};

// pass
will(bad).throw();

// fail
will(good).throw();

Utilities

addTest

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) with this.isTrue and this.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
;'

About

A simple, readable assertion library

Resources

Stars

Watchers

Forks

Packages

No packages published
0