UNPKG

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