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:
85 * assert.arrayOfBuffer
87 * assert.arrayOfNumber
88 * assert.arrayOfObject
89 * assert.arrayOfString
90 * assert.arrayOfStream
93 * assert.optionalArray
95 * assert.optionalBuffer
97 * assert.optionalNumber
98 * assert.optionalObject
99 * assert.optionalString
100 * assert.optionalStream
101 * assert.optionalDate
102 * assert.optionalUuid
103 * assert.optionalArrayOfArray
104 * assert.optionalArrayOfBool
105 * assert.optionalArrayOfBuffer
106 * assert.optionalArrayOfFunc
107 * assert.optionalArrayOfNumber
108 * assert.optionalArrayOfObject
109 * assert.optionalArrayOfString
110 * assert.optionalArrayOfStream
111 * assert.optionalArrayOfDate
112 * assert.optionalArrayOfUuid
113 * assert.AssertionError
119 * assert.notDeepEqual
121 * assert.notStrictEqual
123 * assert.doesNotThrow
128 npm install assert-plus
132 The MIT License (MIT)
133 Copyright (c) 2012 Mark Cavage
135 Permission is hereby granted, free of charge, to any person obtaining a copy of
136 this software and associated documentation files (the "Software"), to deal in
137 the Software without restriction, including without limitation the rights to
138 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
139 the Software, and to permit persons to whom the Software is furnished to do so,
140 subject to the following conditions:
142 The above copyright notice and this permission notice shall be included in all
143 copies or substantial portions of the Software.
145 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
146 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
147 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
148 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
149 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
150 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
155 See <https://github.com/mcavage/node-assert-plus/issues>.