3 This library is a super small wrapper over node's assert module that has two
4 things: (1) the ability to disable assertions with the environment variable
5 NODE\_NDEBUG, and (2) some API wrappers for argument testing. Like
6 `assert.string(myArg, 'myArg')`. As a simple example, most of my code looks
10 var assert = require('assert-plus');
12 function fooAccount(options, callback) {
13 assert.object(options, 'options');
14 assert.number(options.id, 'options.id');
15 assert.bool(options.isManager, 'options.isManager');
16 assert.string(options.name, 'options.name');
17 assert.arrayOfString(options.email, 'options.email');
18 assert.func(callback, 'callback');
27 All methods that *aren't* part of node's core assert API are simply assumed to
28 take an argument, and then a string 'name' that's not a message; `AssertionError`
29 will be thrown if the assertion fails with a message like:
31 AssertionError: foo (string) is required
32 at test (/home/mark/work/foo/foo.js:3:9)
33 at Object.<anonymous> (/home/mark/work/foo/foo.js:15:1)
34 at Module._compile (module.js:446:26)
35 at Object..js (module.js:464:10)
36 at Module.load (module.js:353:31)
37 at Function._load (module.js:311:12)
38 at Array.0 (module.js:484:10)
39 at EventEmitter._tickCallback (node.js:190:38)
45 assert.string(foo, 'foo');
49 There you go. You can check that arrays are of a homogeneous type with `Arrayof$Type`:
53 assert.arrayOfString(foo, 'foo');
57 You can assert IFF an argument is not `undefined` (i.e., an optional arg):
60 assert.optionalString(foo, 'foo');
63 Lastly, you can opt-out of assertion checking altogether by setting the
64 environment variable `NODE_NDEBUG=1`. This is pseudo-useful if you have
65 lots of assertions, and don't want to pay `typeof ()` taxes to v8 in
66 production. Be advised: The standard functions re-exported from `assert` are
67 also disabled in assert-plus if NDEBUG is specified. Using them directly from
68 the `assert` module avoids this behavior.
70 The complete list of APIs is:
86 * assert.arrayOfBuffer
88 * assert.arrayOfNumber
89 * assert.arrayOfFinite
90 * assert.arrayOfObject
91 * assert.arrayOfString
92 * assert.arrayOfStream
94 * assert.arrayOfRegexp
96 * assert.optionalArray
98 * assert.optionalBuffer
100 * assert.optionalNumber
101 * assert.optionalFinite
102 * assert.optionalObject
103 * assert.optionalString
104 * assert.optionalStream
105 * assert.optionalDate
106 * assert.optionalRegexp
107 * assert.optionalUuid
108 * assert.optionalArrayOfArray
109 * assert.optionalArrayOfBool
110 * assert.optionalArrayOfBuffer
111 * assert.optionalArrayOfFunc
112 * assert.optionalArrayOfNumber
113 * assert.optionalArrayOfFinite
114 * assert.optionalArrayOfObject
115 * assert.optionalArrayOfString
116 * assert.optionalArrayOfStream
117 * assert.optionalArrayOfDate
118 * assert.optionalArrayOfRegexp
119 * assert.optionalArrayOfUuid
120 * assert.AssertionError
126 * assert.notDeepEqual
128 * assert.notStrictEqual
130 * assert.doesNotThrow
135 npm install assert-plus
139 The MIT License (MIT)
140 Copyright (c) 2012 Mark Cavage
142 Permission is hereby granted, free of charge, to any person obtaining a copy of
143 this software and associated documentation files (the "Software"), to deal in
144 the Software without restriction, including without limitation the rights to
145 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
146 the Software, and to permit persons to whom the Software is furnished to do so,
147 subject to the following conditions:
149 The above copyright notice and this permission notice shall be included in all
150 copies or substantial portions of the Software.
152 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
153 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
154 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
155 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
156 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
157 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
162 See <https://github.com/mcavage/node-assert-plus/issues>.