assert.doesNotThrow(fn[, error][, message])
fn
<Function>error
<RegExp> | <Function>message
<string>
断言函数 fn
不会抛出错误。
使用 assert.doesNotThrow()
实际上没有用,因为捕获错误然后重新抛出它没有任何好处。
相反,请考虑在不应该抛出的特定代码路径旁边添加注释,并尽可能保持错误消息的表现力。
当 assert.doesNotThrow()
被调用时,它会立即调用 fn
函数。
如果抛出错误并且它与 error
参数指定的类型相同,则抛出 AssertionError
。
如果错误属于不同类型,或者 error
参数未定义,则错误将传播回调用者。
如果指定,则 error
可以是 Class
、RegExp
或验证函数。
有关详细信息,请参阅 assert.throws()
。
例如,以下将抛出 TypeError
,因为断言中没有匹配的错误类型:
import assert from 'node:assert/strict';
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
SyntaxError,
);
const assert = require('node:assert/strict');
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
SyntaxError,
);
但是,以下将导致使用消息 'Got unwanted exception...' 的 AssertionError
:
import assert from 'node:assert/strict';
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
TypeError,
);
const assert = require('node:assert/strict');
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
TypeError,
);
如果抛出 AssertionError
并且为 message
参数提供了值,则 message
的值将附加到 AssertionError
消息:
import assert from 'node:assert/strict';
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
/Wrong value/,
'Whoops',
);
// 抛出: AssertionError: Got unwanted exception: Whoops
const assert = require('node:assert/strict');
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
/Wrong value/,
'Whoops',
);
// 抛出: AssertionError: Got unwanted exception: Whoops
fn
<Function>error
<RegExp> | <Function>message
<string>
Asserts that the function fn
does not throw an error.
Using assert.doesNotThrow()
is actually not useful because there
is no benefit in catching an error and then rethrowing it. Instead, consider
adding a comment next to the specific code path that should not throw and keep
error messages as expressive as possible.
When assert.doesNotThrow()
is called, it will immediately call the fn
function.
If an error is thrown and it is the same type as that specified by the error
parameter, then an AssertionError
is thrown. If the error is of a
different type, or if the error
parameter is undefined, the error is
propagated back to the caller.
If specified, error
can be a Class
, RegExp
, or a validation
function. See assert.throws()
for more details.
The following, for instance, will throw the TypeError
because there is no
matching error type in the assertion:
import assert from 'node:assert/strict';
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
SyntaxError,
);
const assert = require('node:assert/strict');
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
SyntaxError,
);
However, the following will result in an AssertionError
with the message
'Got unwanted exception...':
import assert from 'node:assert/strict';
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
TypeError,
);
const assert = require('node:assert/strict');
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:
import assert from 'node:assert/strict';
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
/Wrong value/,
'Whoops',
);
// Throws: AssertionError: Got unwanted exception: Whoops
const assert = require('node:assert/strict');
assert.doesNotThrow(
() => {
throw new TypeError('Wrong value');
},
/Wrong value/,
'Whoops',
);
// Throws: AssertionError: Got unwanted exception: Whoops