Minimalistic BDD assertion toolkit based on should.js
expect(window.r).to.be(undefined);
expect({ a: 'b' }).to.eql({ a: 'b' })
expect(5).to.be.a('number');
expect([]).to.be.an('array');
expect(window).not.to.be.an(Image);
Reference Automattic#100 It seems that https://github.com/LearnBoost is too busy with his work, and our beloved assertion library has not been updated for months, so I decided to fork it to my own Github account and started reviewing all PR, one by one.
be
is no longer a method, i.e you can not usebe()
eql' is now an alias
equal`, which you usually want to use for equality assertion, except in the next 2 following casesresemble
is use to assert if 2 object looks like each other, i.e.==
comparisonidentical
is use to assert if 2 variables refer to the same object, i.e.
expect([]).not.to.be.identical([]);
var arr = [];
expect(arr).not.to.be.identical(arr);
- We shall contact https://www.npmjs.org/~onirame to claim npm package name
expect
, as this package was last updated 2 years ago, ref. So you might in the future usenpm install expect
andvar expect = require('expect')
. In the mean time, in your package.json, you can use"mocha": "git://github.com/truongsinh/expect"
Ideas and PRs are welcome
-
If you are fixing a bug, do it in BDD manner, i.e. write a test case that fails first, then modify the library such that it passes that test case.
-
If you are developing a new feature, ask yourself should it be in core or a plugin. Issues page is the best place to discuss. We shall keep a list of available plugin in this README (or a separate list in a far future when it is too crowded). Take a look at
expect.core
to see how "core" plugin is implemented. -
Fell free to bump package.json, bower.json and expect.version build number, for example "0.2.1-dev", and remember to keep it consistent between the 3.
-
All PR are subject to be modified, such as code format, version number.
- Cross-browser: works on IE6+, Firefox, Safari, Chrome, Opera.
- Compatible with all test frameworks.
- Node.JS ready (
require('expect.js')
). - Standalone. Single global with no prototype extensions or shims.
Install it with NPM or add it to your package.json
:
$ npm install expect.js
Then:
var expect = require('expect.js');
Expose the expect.js
found at the top level of this repository.
<script src="expect.js"></script>
There are 4 terminal flag: ok
, throw
, empty
, fail
. The rest are chainable, i.e. you can use and
after them.
and: make chainable asserts
expect([1, 2]).not.be.empty().and.to.contain(1).and.to.contain(2).and.not.to.contain(3);
ok: asserts that the value is truthy or not.
expect(1).to.be.ok();
expect(true).to.be.ok();
expect({}).to.be.ok();
expect(0).to.not.be.ok();
equal / eql: asserts that two objects are equivalent
expect(1).to.equal(1);
expect(1.4 - 0.1).to.not.equal(1.3); // see approximate
expect(NaN).not.to.equal(NaN);
expect(1).not.to.equal(true);
expect('1').to.not.equal(1);
expect({a: 3}).to.equal({a: 3}); // see identical
expect({a: 3}).not.to.equal({a: 4});
expect({a: 3}).not.to.equal({b: 3});
approximately / approximate: handy for float rounding error.
expect(1.4 - 0.1).to.be.approximately(1.3, 1e-15);
expect(99.99).approximate(100, 0.1);
resemble: asserts loose equality ==
expect(1).to.resemble('1');
expect(0).to.resemble(false);
a/an: asserts typeof
with support for array
type and instanceof
.
// typeof with optional `array`
expect(5).to.be.a('number');
expect([]).to.be.an('array'); // works
expect([]).to.be.an('object'); // works too, since it uses `typeof`
// constructors
expect(5).to.be.a(Number);
expect([]).to.be.an(Array);
expect(tobi).to.be.a(Ferret);
expect(person).to.be.a(Mammal);
match: asserts String
regular expression match.
expect(program.version).to.match(/[0-9]+\.[0-9]+\.[0-9]+/);
contain: asserts indexOf for an array or string, or child key-value for an object.
expect([1, 2]).to.contain(1);
expect('hello world').to.contain('world');
expect({ foo: 'bar', bar: 'foo' }).to.contain({ bar: 'foo' });
expect({ foo: 'bar' }).to.not.contain({ foo: 'baz' });
length: asserts array .length
.
expect([]).to.have.length(0);
expect([1,2,3]).to.have.length(3);
empty: asserts that an array is empty or not.
expect([]).to.be.empty();
expect({}).to.be.empty();
expect({ length: 0, duck: 'typing' }).to.be.empty();
expect({ my: 'object' }).to.not.be.empty();
expect([1,2,3]).to.not.be.empty();
property: asserts presence of an own property (and value optionally).
expect(window).to.have.property('expect')
expect(window).to.have.property('expect', expect)
expect({a: 'b'}).to.have.property('a');
key/keys: asserts the presence of a key. Supports the only
modifier.
expect({ a: 'b' }).to.have.key('a');
expect({ a: 'b', c: 'd' }).to.only.have.keys('a', 'c');
expect({ a: 'b', c: 'd' }).to.only.have.keys(['a', 'c']);
expect({ a: 'b', c: 'd' }).to.not.only.have.key('a');
throwException/throwError: asserts that the Function
throws or not when called.
expect(fn).to.throwError(); // synonym of throwException
expect(fn).to.throwException(function (e) { // get the exception object
expect(e).to.be.a(SyntaxError);
});
expect(fn).to.throwException(/matches the exception message/);
expect(fn2).to.not.throwException();
withArgs: creates anonymous function to call fn with arguments.
expect(fn).withArgs(invalid, arg).to.throwException();
expect(fn).withArgs(valid, arg).to.not.throwException();
within/between: asserts a number within a range.
expect(1).to.be.within(0, Infinity)
expect(2).to.be.between(1, 3);
greaterThan/above: asserts >
.
expect(3).to.be.above(0);
expect(5).to.be.greaterThan(3);
lessThan/below: asserts <
.
expect(0).to.be.below(3);
expect(1).to.be.lessThan(3);
fail: explicitly forces failure.
expect().fail()
expect().fail("Custom failure message")
For example, if you create a test suite with mocha.
Let's say we wanted to test the following program:
math.js
function add (a, b) { return a + b; };
Our test file would look like this:
describe('test suite', function () {
it('should expose a function', function () {
expect(add).to.be.a('function');
});
it('should do math', function () {
expect(add(1, 3)).to.equal(4);
});
});
If a certain expectation fails, an exception will be raised which gets captured and shown/processed by the test runner.
- No need for static
should
methods likeshould.strictEqual
. For example,expect(obj).to.be(undefined)
works well. - Some API simplifications / changes.
- API changes related to browser compatibility.
Clone the repository and install the developer dependencies:
git clone git://github.com/LearnBoost/expect.js.git expect
cd expect && npm install
make test
make test-browser
and point your browser(s) to http://localhost:3000/test/
(The MIT License)
Copyright (c) 2014 TruongSinh Tran-Nguyen <[email protected]> (c) 2011 Guillermo Rauch <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Heavily borrows from should.js by TJ Holowaychuck - MIT.