1 _should_ is an expressive, test framework agnostic, assertion library for [node](http://nodejs.org).
3 _should_ literally extends node's _assert_ module, in fact, it is node's assert module, for example `should.equal(str, 'foo')` will work, just as `assert.equal(str, 'foo')` would, and `should.AssertionError` **is** `asset.AssertionError`, meaning any test framework supporting this constructor will function properly with _should_.
9 , pets: ['tobi', 'loki', 'jane', 'bandit']
12 user.should.have.property('name', 'tj');
13 user.should.have.property('pets').with.lengthOf(4)
21 _should_'s assertion chaining provides an expressive way to build up an assertion, along with dummy getters such as _an_, _have_, and _be_, provided are what I am simply calling **modifiers**, which have a meaning effect on the assertion. An example of this is the _not_ getter, which negates the meaning, aka `user.should.not.have.property('name')`. In the previous example note the use of _have_, as we could omit it and still construct a valid assertion.
23 Some modifiers such as _include_ only have an effect with specific assertion methods, for example when asserting a substring like so: `str.should.include.string('test')`, we could omit _include_, but it helps express the meaning, however _keys_ has a strict effect, unless the _include_ modifier is used.
25 ## chaining assertions
27 Some assertions can be chained, for example if a property is volatile we can first assert property existence:
29 user.should.have.property('pets').with.lengthOf(4)
31 which is essentially equivalent to below, however the property may not exist:
33 user.pets.should.have.lengthOf(4)
35 our dummy getters such as _and_ also help express chaining:
37 user.should.be.a('object').and.have.property('name', 'tj')
49 false.should.not.be.ok
58 '1'.should.not.be.true
65 (0).should.not.be.false
71 var args = (function(){ return arguments; })(1,2,3);
72 args.should.be.arguments;
73 [].should.not.be.arguments;
77 Asserts that length is 0:
81 ({ length: 0 }).should.be.empty
87 ({ foo: 'bar' }).should.eql({ foo: 'bar' })
88 [1,2,3].should.eql([1,2,3])
94 should.strictEqual(undefined, value)
95 should.strictEqual(false, value)
97 'test'.should.equal('test')
98 [1,2,3].should.not.equal([1,2,3])
102 Assert inclusive numeric range:
104 user.age.should.be.within(5, 50)
110 user.should.be.a('object')
111 'test'.should.be.a('string')
115 Assert __instanceof__:
117 user.should.be.an.instanceof(User)
118 [].should.be.an.instanceof(Array)
122 Assert numeric value above the given value:
124 user.age.should.be.above(5)
125 user.age.should.not.be.above(100)
129 Assert numeric value below the given value:
131 user.age.should.be.below(100)
132 user.age.should.not.be.below(5)
138 username.should.match(/^\w+$/)
142 Assert _length_ property exists and has a value of the given number:
144 user.pets.should.have.length(5)
145 user.pets.should.have.a.lengthOf(5)
153 'foobar'.should.include.string('foo')
154 'foobar'.should.include.string('bar')
155 'foobar'.should.not.include.string('baz')
159 Assert property exists and has optional value:
161 user.should.have.property('name')
162 user.should.have.property('age', 15)
163 user.should.not.have.property('rawr')
164 user.should.not.have.property('age', 0)
168 Assert own property (on the immediate object):
170 ({ foo: 'bar' }).should.have.ownProperty('foo')
176 [1,2,3].should.contain(3)
177 [1,2,3].should.contain(2)
178 [1,2,3].should.not.contain(4)
182 Assert own object keys, which must match _exactly_,
183 and will fail if you omit a key or two:
185 var obj = { foo: 'bar', baz: 'raz' };
186 obj.should.have.keys('foo', 'bar');
187 obj.should.have.keys(['foo', 'bar']);
189 using the _include_ modifier, we can check inclusion of a key,
190 but not fail when we omit a few:
192 obj.should.include.keys('foo')
193 obj.should.include.keys('bar')
194 obj.should.not.include.keys('baz')
198 Assert that the given property is a function:
200 user.should.respondTo('email')
204 For example you can use should with the [Expresso TDD Framework](http://github.com/visionmedia/expresso) by simply including it:
206 var lib = require('mylib')
207 , should = require('should');
210 'test .version': function(){
211 lib.version.should.match(/^\d+\.\d+\.\d+$/);
217 To run the tests for _should_ simple update your git submodules and run:
221 ## OMG IT EXTENDS OBJECT???!?!@
223 Yes, yes it does, with a single getter _should_, and no it wont break your code, because it does this **properly** with a non-enumerable property.
229 Copyright (c) 2010 TJ Holowaychuk <tj@vision-media.ca>
231 Permission is hereby granted, free of charge, to any person obtaining
232 a copy of this software and associated documentation files (the
233 'Software'), to deal in the Software without restriction, including
234 without limitation the rights to use, copy, modify, merge, publish,
235 distribute, sublicense, and/or sell copies of the Software, and to
236 permit persons to whom the Software is furnished to do so, subject to
237 the following conditions:
239 The above copyright notice and this permission notice shall be
240 included in all copies or substantial portions of the Software.
242 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
243 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
244 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
245 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
246 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
247 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
248 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.