npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

willy

v1.0.3

Published

An assertion library that reads very naturally.

Downloads

30

Readme

Build Status

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 = require('willy').will;

describe('some test suite', function () {
    it('should do X, Y, and Z', function () {

        // Will it?
        will(true).be(true);

    });
});

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.

// Chai
expect(foo).to.be.an.instanceof(Foo);

// Shouldjs
foo.should.be.an.instanceOf(Foo)

// Jasmine
expect(foo).toEqual(jasmine.any(Foo));

// Willy
will(foo).beA(Foo);

Want to add to Willy's repertoire? That's easy, too.

willy.define(function beASubstringOf() {
    return this.expected.indexOf(this.actual) > -1;
});

will('potato').beASubstringOf('Bender Bending Rodriguez');
// 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
will(3).be(3);

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

beA/beAn

Test inheritance. This also handles some of the weirdness of JavaScript, so that String and Number literals act as expected.

var Foo = function () {};
var foo = new Foo();

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

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

beDefined

Test for a defined value.

var foo = 123;
var bar;

// pass
will(foo).beDefined();

// fail
will(bar).beDefined();

beFalsy

Test for a falsy value.

// pass
will('').beFalsy();

// fail
will('asdf').beFalsy();

beGreaterThan

Test a value to see if it's greater than another.

// pass
will(4).beGreaterThan(3);

// fail
will(3).beGreaterThan(3);

beLessThan

Test a value to see if it's less than another.

// pass
will(3).beLessThan(4);

// fail
will(3).beLessThan(3);

beLike

Test equality.

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

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

This also works on objects, including Arrays, recursively.

// pass
will({
    foo: 'bar',
    baz: [1, 2, 3],
    quux: null
}).beLike({
    foo: 'bar',
    baz: [1, 2, 3],
    quux: null
});

will([1, 2, 3]).beLike([1, 2, 3]);

// fail
will({
    foo: {
        bar: {
            baz: {
                quux: true
            }
        }
    }
}).beLike({
    foo: {
        bar: {
            baz: {
                quux: false
            }
        }
    }
});

will([1, 2, 3]).beLike([3, 2, 1]);

beMoreThan

Test a value to see if it's more than another.

// pass
will(4).beMoreThan(3);

// fail
will(3).beMoreThan(3);

beNull

Test for null.

// pass
will(null).beNull();

// fail
will(undefined).beNull();

beTruthy

Test for a truthy value.

// pass
will('asdf').beTruthy();

// fail
will('').beTruthy();

beUndefined

Test for an undefined value.

var foo;
var bar = 123;

// pass
will(foo).beUndefined();

// fail
will(bar).beUndefined();

exist

Test the existence of a property.

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

will(foo.bar).exist();

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

have

Test the existence of multiple 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

Test 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

Test an 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

Test for an own property.

var Foo = function () {};
var foo = new Foo();

foo.bar = true;
Foo.prototype.baz = true;

// pass
will(foo).haveOwn('bar');

// fail
will(foo).haveOwn('baz');

match

Test against RegExp.


// pass
will('asdf').match(/SD/i);

// fail
will('asdf').match(/SD/);

throw

Test for an error being thrown.

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

var good = function () {};

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

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

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.define(function beASubstringOf() {
    return this.expected.indexOf(this.actual) > -1;
});

will('potato').beASubstringOf('Bender Bending Rodriguez');
// expected 'potato' to be a substring of 'Bender Bending Rodriguez'

Inside every test function

  • this.actual is the value passed to will()
  • 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 explanation
willy.define({
    fn: function beASubstringOf() {
        return this.expected.indexOf(this.actual) > -1;
    },
    explanation: 'be found inside of'
});

will('potato').beASubstringOf('Bender Bending Rodriguez');
// expected 'potato' to be found inside of 'Bender Bending Rodriguez'

Changing the Name

// providing a different name
willy.define({
    fn: function () {
        return this.expected.indexOf(this.actual) > -1;
    },
    name: 'lieWithin'
});

will('potato').lieWithin('Bender Bending Rodriguez');
// 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.loadDefinitions({
    beASubstringOf: {
        fn: function () {
            return this.expected.indexOf(this.actual) > -1;
        },
        explanation: 'be found inside of'
    },

    lieWithin: {
        fn: function () {
            return this.expected.indexOf(this.actual) > -1;
        }
    }
});

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

exports.beASubstringOf = {
    fn: function () {
        return this.expected.indexOf(this.actual) > -1;
    },
    explanation: 'be found inside of'
};

exports.lieWithin = {
    fn: function () {
        return this.expected.indexOf(this.actual) > -1;
    }
};

A Test Suite

var willy = require('willy'),
    will = willy.will;

willy.loadDefinitions(require('./my-tests.js'));

describe('a test suite', function () {
    it('should be pretty easy to share custom tests', function () {
        will('potato').lieWithin('Bender Bending Rodriguez');
        // expected 'potato' to lie within 'Bender Bending Rodriguez'
    });
});