test framework agnostic BDD-style assertions
should is an expressive, readable, framework-agnostic assertion library. The main goals of this library are to be expressive and to be helpful. It keeps your test code clean, and your error messages helpful.
should extends the
Object.prototype with a single non-enumerable getter that allows you to express how that object should behave. It also returns itself when required with
require. It does not mean that you should use it like getter - it is possible to use it as function call. For this use
Please check wiki page for upgrading instructions.
var should = require'should';var user =name: 'tj'pets: 'tobi' 'loki' 'jane' 'bandit';usershouldhaveproperty'name' 'tj';usershouldhaveproperty'pets'withlengthOf4;// If the object was created with Object.create(null)// then it doesn't inherit `Object.prototype`, so it will not have `.should` getter// so you can do:shoulduserhaveproperty'name' 'tj';// also you can test in that way for null'sshouldnullnotbeok;someAsyncTaskfooshouldnotexisterr;shouldexistresult;resultbarshouldequalfoo;;
$ npm install should --save-dev
Require it and use:
var should = require'should';5shouldbeexactly5andbeaNumber;
Well, even when browsers by complains of authors has 100% es5 support, it does not mean it has not bugs. Please see wiki for known bugs.
If you want to use should in browser, use the
should.js file in the root of this repository, or build it yourself. To build a fresh version:
$ npm install$ gulp script
The script is exported to
window.Should. It is the same as using
Also, in the case of node.js,
Object.prototype is extended with
should (hence the capital S in
windowshouldbeexactlywindow;// the same// window is host objectshouldbeexactlywindow;// you should not really care about it5shouldbeexactly5;
You can easy install it with npm or bower:
npm install should --save-dev# orbower install shouldjs/should.js
Actual api docs generated by jsdoc comments and available at http://shouldjs.github.io.
Please look on usage in examples
.not negates the current assertion.
Every assertion will return a
should.js-wrapped Object, so assertions can be chained.
To help chained assertions read more clearly, you can use the following helpers anywhere in your chain:
.which. Use them for better readability; they do nothing at all.
Almost all assertions return the same object - so you can easy chain them. But some (eg:
.property) move the assertion object to a property value, so be careful.
To add own assertion you need to call
should.Assertion.add function. It accept 3 arguments:
What assertion function should do. It should check only positive case.
should will handle
this in assertion function will be instance of
should.Assertion and you must define in any way this.params object
in your assertion function call before assertion check happen.
params object can contain several fields:
operator- it is string which describe your assertion
actualit is actual value, you can assume it is your own this.obj if you need to define you own
expectedit is any value that expected to be matched this.obj
You can assume its usage in generating AssertionError message like: expected
obj? || this.obj not?
should sources appeared 2 kinds of usage of this method.
First not preferred and used only for shortcuts to other assertions, e.g how
There you can see that assertion function do not define own
this.params and instead call within the same assertion
that will fill
this.params. You should use this way very carefully, but you can use it.
Second way preferred and i assume you will use it instead of first.
Assertionadd'true'thisparams = operator: 'to be true' expected: true ;thisobjshouldbeexactlytrue;true;
in this case this.params defined and then used new assertion context (because called
.should). Internally this way does not
create any edge cases as first.
Assertionadd'asset'thisparams = operator: 'to be asset' ;thisobjshouldhaveproperty'id'whichisaNumber;thisobjshouldhaveproperty'path';//then> id: '10' shouldbeanasset;AssertionError: expected id: '10' to be assetexpected '10' to be a number> id: 10 shouldbeanasset;AssertionError: expected id: 10 to be assetexpected id: 10 to have property path
Actual list of contributors if you want to show it your friends.
To run the tests for should simply run:
$ make test
See also CONTRIBUTING.
Yes, yes it does, with a single getter should, and no it won't break your code, because it does this properly with a non-enumerable property.
Also it is possible use it without extension.
MIT © 2010-2014 TJ Holowaychuk