1 | # assert
|
2 |
|
3 | [![Build Status](https://travis-ci.org/defunctzombie/commonjs-assert.png?branch=master)](https://travis-ci.org/defunctzombie/commonjs-assert)
|
4 |
|
5 | This module is used for writing unit tests for your applications, you can access it with require('assert').
|
6 |
|
7 | The API is derived from the [commonjs 1.0 unit testing](http://wiki.commonjs.org/wiki/Unit_Testing/1.0) spec and the [node.js assert module](http://nodejs.org/api/assert.html)
|
8 |
|
9 | ## assert.fail(actual, expected, message, operator)
|
10 | Throws an exception that displays the values for actual and expected separated by the provided operator.
|
11 |
|
12 | ## assert(value, message), assert.ok(value, [message])
|
13 | Tests if value is truthy, it is equivalent to assert.equal(true, !!value, message);
|
14 |
|
15 | ## assert.equal(actual, expected, [message])
|
16 | Tests shallow, coercive equality with the equal comparison operator ( == ).
|
17 |
|
18 | ## assert.notEqual(actual, expected, [message])
|
19 | Tests shallow, coercive non-equality with the not equal comparison operator ( != ).
|
20 |
|
21 | ## assert.deepEqual(actual, expected, [message])
|
22 | Tests for deep equality.
|
23 |
|
24 | ## assert.notDeepEqual(actual, expected, [message])
|
25 | Tests for any deep inequality.
|
26 |
|
27 | ## assert.strictEqual(actual, expected, [message])
|
28 | Tests strict equality, as determined by the strict equality operator ( === )
|
29 |
|
30 | ## assert.notStrictEqual(actual, expected, [message])
|
31 | Tests strict non-equality, as determined by the strict not equal operator ( !== )
|
32 |
|
33 | ## assert.throws(block, [error], [message])
|
34 | Expects block to throw an error. error can be constructor, regexp or validation function.
|
35 |
|
36 | Validate instanceof using constructor:
|
37 |
|
38 | ```javascript
|
39 | assert.throws(function() { throw new Error("Wrong value"); }, Error);
|
40 | ```
|
41 |
|
42 | Validate error message using RegExp:
|
43 |
|
44 | ```javascript
|
45 | assert.throws(function() { throw new Error("Wrong value"); }, /value/);
|
46 | ```
|
47 |
|
48 | Custom error validation:
|
49 |
|
50 | ```javascript
|
51 | assert.throws(function() {
|
52 | throw new Error("Wrong value");
|
53 | }, function(err) {
|
54 | if ( (err instanceof Error) && /value/.test(err) ) {
|
55 | return true;
|
56 | }
|
57 | }, "unexpected error");
|
58 | ```
|
59 |
|
60 | ## assert.doesNotThrow(block, [message])
|
61 | Expects block not to throw an error, see assert.throws for details.
|
62 |
|
63 | ## assert.ifError(value)
|
64 | Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, error in callbacks.
|