LearnBoost/expect.js fork
xpect - AMinimalistic BDD assertion toolkit based on should.js
to;totobe;tobe;nottobe;
Features
expect
- Cross-browser: works on IE6+, Firefox, Safari, Chrome, Opera.
- Compatible with all test frameworks.
- Node.JS ready (
npm install xpect
). - Standalone. Single global with no prototype extensions or shims.
additional features beyond expect
- AMD support (not easy to achieve with
expect.js
because of the.js
prefix in its package name) - AMD, CommonJS and Browser-Globals support by UMD
- bower.io support (
bower install xpect
) - component support (
component install xpect
) - and, most importantly, same AMD and CommonJS package name
How to use
CommonJS (Node.js)
var xpect = ;tobe;
Browser Globals
AMD
;
API
ok: asserts that the value is truthy or not
tobe;tobe;tobe;tonotbe;
be / equal: asserts ===
equality
tonotto;nottotonot;
eql: asserts loose equality that works with objects
to;to;
a/an: asserts typeof
with support for array
type and instanceof
// typeof with optional `array`tobe;tobe; // workstobe; // works too, since it uses `typeof` // constructorstobe;tobe;tobe;tobe;
match: asserts String
regular expression match
to;
contain: asserts indexOf for an array or string
to;to;
length: asserts array .length
tohavelength0;tohavelength3;
empty: asserts that an array is empty or not
tobe;tobe;tobe;tonotbe;tonotbe;
property: asserts presence of an own property (and value optionally)
tohavetohavetohave;
key/keys: asserts the presence of a key. Supports the only
modifier
tohave;toonlyhave;toonlyhave;tonotonlyhave;
throwException/throwError: asserts that the Function
throws or not when called
to; // synonym of throwExceptionto;to;tonot;
withArgs: creates anonymous function to call fn with arguments
to;tonot;
within: asserts a number within a range
tobe;
greaterThan/above: asserts >
tobeabove0;tobe;
lessThan/below: asserts <
tobebelow3;tobe;
fail: explicitly forces failure.
Using with a test framework
For example, if you create a test suite with mocha.
Let's say we wanted to test the following program:
math.js
{ return a + b; };
Our test file would look like this:
;
If a certain xpectation fails, an exception will be raised which gets captured and shown/processed by the test runner.
Differences with should.js
- No need for static
should
methods likeshould.strictEqual
. For example,xpect(obj).to.be(undefined)
works well. - Some API simplifications / changes.
- API changes related to browser compatibility.
Running tests
# install xpect and devDependencies npm install xpectcd node_modules/xpectnpm installnpm run install-browser-test # node npm test # browser npm run browser-testxdg-open http://localhost:3000/test/
Credits
(The MIT License)
Copyright (c) 2011 Guillermo Rauch <guillermo@learnboost.com>
Portions (c) 2014 Michael Mayer <michael@schnittstabil.de>
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.
3rd-party
Heavily borrows from should.js by TJ Holowaychuck - MIT.