Docs Nodejs 10 15
Docs Nodejs 10 15
);
However, the following will result in an AssertionError with the message 'Got unwanted
exception...':
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
TypeError
);
If an AssertionError is thrown and a value is provided for the message parameter, the value of
message will be appended to the AssertionError message:
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
/Wrong value/,
'Whoops'
);
// Throws: AssertionError: Got unwanted exception: Whoops
assert.equal(actual, expected[, message])#
History
actual <any>
expected <any>
message <string> | <Error>
Strict assertion mode
An alias of assert.strictEqual().
assert.equal(1, 1);
// OK, 1 == 1
assert.equal(1, '1');
// OK, 1 == '1'
assert.equal(NaN, NaN);
// OK
assert.equal(1, 2);
// AssertionError: 1 == 2
assert.equal({ a: { b: 1 } }, { a: { b: 1 } });
// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }
If the values are not equal, an AssertionError is thrown with a message property set equal to the
value of the message parameter. If the message parameter is undefined, a default error message is
assigned. If the message parameter is an instance of an Error then it will be thrown instead of the
AssertionError.
assert.fail([message])#
Added in: v0.1.21
message <string> | <Error> Default: 'Failed'
Throws an AssertionError with the provided error message or a default error message. If the
message parameter is an instance of an Error then it will be thrown instead of the AssertionError.
assert.fail();
// AssertionError [ERR_ASSERTION]: Failed
assert.fail('boom');
// AssertionError [ERR_ASSERTION]: boom
assert.fail('a', 'b');
// AssertionError [ERR_ASSERTION]: 'a' != 'b'
assert.fail(1, 2, 'fail');
// AssertionError [ERR_ASSERTION]: fail
assert.fail(1, 2, 'whoops', '>');
// AssertionError [ERR_ASSERTION]: whoops
function suppressFrame() {
assert.fail('a', 'b', undefined, '!==', suppressFrame);
}
suppressFrame();
// AssertionError [ERR_ASSERTION]: 'a' !== 'b'
// at repl:1:1
// at ContextifyScript.Script.runInThisContext (vm.js:44:33)
// ...
assert.ifError(value)#
History
value <any>
Throws value if value is not undefined or null. This is useful when testing the error argument in
callbacks. The stack trace contains all frames from the error passed to ifError() including the
potential new frames for ifError() itself.
assert.ifError(null);
// OK
assert.ifError(0);
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0
assert.ifError('error');
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'
assert.ifError(new Error());
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error
(function ifErrorFrame() {
assert.ifError(err);
})();
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error
// at ifErrorFrame
// at errorFrame
assert.match(string, regexp[, message])#
History
string <string>
regexp <RegExp>
message <string> | <Error>
Expects the string input to match the regular expression.
assert.match(123, /pass/);
// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.
An alias of assert.notDeepStrictEqual().
const obj1 = {
a: {
b: 1
}
};
const obj2 = {
a: {
b: 2
}
};
const obj3 = {
a: {
b: 1
}
};
const obj4 = Object.create(obj1);
assert.notDeepEqual(obj1, obj1);
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
assert.notDeepEqual(obj1, obj2);
// OK
assert.notDeepEqual(obj1, obj3);
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
assert.notDeepEqual(obj1, obj4);
// OK
If the values are deeply equal, an AssertionError is thrown with a message property set equal to the
value of the message parameter. If the message parameter is undefined, a default error message is
assigned. If the message parameter is an instance of an Error then it will be thrown instead of the
AssertionError.
An alias of assert.notStrictEqual().