summaryrefslogtreecommitdiff
path: root/node_modules/should/Readme.md
blob: 2036df13d8fed1f9791d2430ad293ec0fb0226c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
_should_ is an expressive, readable, test framework agnostic, assertion library for [node](http://nodejs.org).
  
It extends the Object prototype with a single non-enumerable getter that allows you to express how that object should behave.

_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** `assert.AssertionError`, meaning any test framework supporting this constructor will function properly with _should_.

## Example

    var user = {
        name: 'tj'
      , pets: ['tobi', 'loki', 'jane', 'bandit']
    };

    user.should.have.property('name', 'tj');
    user.should.have.property('pets').with.lengthOf(4);
    
    someAsyncTask(foo, function(err, result){
      should.not.exist(err);
      should.exist(result);
      result.bar.should.equal(foo);
    });

## Installation

    $ npm install should

## assert extras

As mentioned above, _should_ extends node's _assert_. The returned object from `require('should')` is thus similar to the returned object from `require('assert')`, but it has one extra convenience method:

    should.exist('hello')
    should.exist([])
    should.exist(null)  // will throw

This is equivalent to `should.ok`, which is equivalent to `assert.ok`, but reads a bit better. It gets better, though:

    should.not.exist(false)
    should.not.exist('')
    should.not.exist({})    // will throw

We may add more _assert_ extras in the future... ;)

## chaining assertions

Some assertions can be chained, for example if a property is volatile we can first assert property existence:

    user.should.have.property('pets').with.lengthOf(4)

which is essentially equivalent to below, however the property may not exist:

    user.pets.should.have.lengthOf(4)

our dummy getters such as _and_ also help express chaining:

    user.should.be.a('object').and.have.property('name', 'tj')

## exist (static)

The returned object from `require('should')` is the same object as `require('assert')`. So you can use `should` just like `assert`:

    should.fail('expected an error!')
    should.strictEqual(foo, bar)

In general, using the Object prototype's _should_ is nicer than using these `assert` equivalents, because _should_ gives you access to the expressive and readable language described above:

    foo.should.equal(bar)   // same as should.strictEqual(foo, bar) above

The only exception, though, is when you can't be sure that a particular object exists. In that case, attempting to access the _should_ property may throw a TypeError:

    foo.should.equal(bar)   // throws if foo is null or undefined!

For this case, `require('should')` extends `require('assert')` with an extra convenience method to check whether an object exists:

    should.exist({})
    should.exist([])
    should.exist('')
    should.exist(0)
    should.exist(null)      // will throw
    should.exist(undefined) // will throw

You can also check the negation:

    should.not.exist(undefined)
    should.not.exist(null)
    should.not.exist('')    // will throw
    should.not.exist({})    // will throw

Once you know an object exists, you can safely use the _should_ property on it.

## ok

Assert truthfulness:

    true.should.be.ok
    'yay'.should.be.ok
    (1).should.be.ok

or negated:

    false.should.not.be.ok
    ''.should.not.be.ok
    (0).should.not.be.ok

## true

Assert === true:

    true.should.be.true
    '1'.should.not.be.true

## false

Assert === false:

     false.should.be.false
     (0).should.not.be.false

## arguments

Assert `Arguments`:

    var args = (function(){ return arguments; })(1,2,3);
    args.should.be.arguments;
    [].should.not.be.arguments;

## empty

Asserts that length is 0:

    [].should.be.empty
    ''.should.be.empty
    ({ length: 0 }).should.be.empty

## eql

equality:

    ({ foo: 'bar' }).should.eql({ foo: 'bar' })
    [1,2,3].should.eql([1,2,3])

## equal

strict equality:

    should.strictEqual(undefined, value)
    should.strictEqual(false, value)
    (4).should.equal(4)
    'test'.should.equal('test')
    [1,2,3].should.not.equal([1,2,3])

## within

Assert inclusive numeric range:

    user.age.should.be.within(5, 50)

## a

Assert __typeof__:

    user.should.be.a('object')
    'test'.should.be.a('string')

## instanceof

Assert __instanceof__:

    user.should.be.an.instanceof(User)
    [].should.be.an.instanceof(Array)

## above

Assert numeric value above the given value:

    user.age.should.be.above(5)
    user.age.should.not.be.above(100)

## below

Assert numeric value below the given value:

    user.age.should.be.below(100)
    user.age.should.not.be.below(5)

## match

Assert regexp match:

    username.should.match(/^\w+$/)

## length

Assert _length_ property exists and has a value of the given number:

    user.pets.should.have.length(5)
    user.pets.should.have.a.lengthOf(5)

Aliases: _lengthOf_

## property

Assert property exists and has optional value:

    user.should.have.property('name')
    user.should.have.property('age', 15)
    user.should.not.have.property('rawr')
    user.should.not.have.property('age', 0)

## ownProperty

Assert own property (on the immediate object):

    ({ foo: 'bar' }).should.have.ownProperty('foo')

## status(code)

 Asserts that `.statusCode` is `code`:

   res.should.have.status(200);

## header(field[, value])

 Asserts that a `.headers` object with `field` and optional `value` are present:

     res.should.have.header('content-length');
     res.should.have.header('Content-Length', '123');
     res.should.have.header('content-length', '123');

## json

  Assert that Content-Type is "application/json; charset=utf-8"
  
      res.should.be.json

## html

  Assert that Content-Type is "text/html; charset=utf-8"
  
      res.should.be.html

## include(obj)

Assert that the given `obj` is present via `indexOf()`, so this works for strings, arrays, or custom objects implementing indexOf:

Assert array value:

    [1,2,3].should.include(3)
    [1,2,3].should.include(2)
    [1,2,3].should.not.include(4)

Assert substring:

    'foo bar baz'.should.include('foo')
    'foo bar baz'.should.include('bar')
    'foo bar baz'.should.include('baz')
    'foo bar baz'.should.not.include('FOO')

## includeEql(obj)

Assert that an object equal to the given `obj` is present in an Array:

    [[1],[2],[3]].should.includeEql([3])
    [[1],[2],[3]].should.includeEql([2])
    [[1],[2],[3]].should.not.includeEql([4])

## throw()

Assert an exception is thrown:

```js
(function(){
  throw new Error('fail');
}).should.throw();
```

Assert an exception is not thrown:

```js
(function(){
 
}).should.not.throw();
```
Assert exepection message matches string:

```js
(function(){
  throw new Error('fail');
}).should.throw('fail');
```

Assert exepection message matches regexp:

```js
(function(){
  throw new Error('failed to foo');
}).should.throw(/^fail/);
```

## keys

Assert own object keys, which must match _exactly_,
and will fail if you omit a key or two:

    var obj = { foo: 'bar', baz: 'raz' };
    obj.should.have.keys('foo', 'bar');
    obj.should.have.keys(['foo', 'bar']);

## Optional Error description

As it can often be difficult to assertain exactly where failed assertions are comming from in your tests, an optional description parameter can be passed to several should matchers. The description will follow the failed assertion in the error:

    (1).should.eql(0, 'some useful description')

    AssertionError: expected 1 to equal 0 | some useful description
      at Object.eql (/Users/swift/code/should.js/node_modules/should/lib/should.js:280:10)
      ...

The methods that support this optional description are: `eql`, `equal`, `within`, `a`, `instanceof`, `above`, `below`, `match`, `length`, `property`, `ownProperty`, `include`, and `includeEql`.

## Express example

For example you can use should with the [Expresso TDD Framework](http://github.com/visionmedia/expresso) by simply including it:

    var lib = require('mylib')
      , should = require('should');
  
    module.exports = {
      'test .version': function(){
        lib.version.should.match(/^\d+\.\d+\.\d+$/);
      }
    };

## Running tests

To run the tests for _should_ simple update your git submodules and run:

    $ make test

## OMG IT EXTENDS OBJECT???!?!@

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.

## License 

(The MIT License)

Copyright (c) 2010-2011 TJ Holowaychuk <tj@vision-media.ca>
Copyright (c) 2011 Aseem Kishore <aseem.kishore@gmail.com>

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.