Node.js assert.doesNotThrow() 函数 assert 作为参数传递给 fn 的函数不会抛出错误。
使用 assert.doesNotThrow() 函数没有用,因为捕获错误并再次抛出它没有适当的好处。相反,当将注释添加到不应抛出的特定代码中时,它非常具有表现力。
语法
以下是 assert.doesNotThrow() 函数Node.js的语法 -
assert.doesNotThrow(fn[, error][, message]);
参数
此函数接受三个参数。下面将对此进行描述。
- fn − (必需) 此参数包含函数 fn,并期望该函数不会引发错误。
- error − (可选) 此参数可以是 RegExp 验证函数。
- message − (可选)字符串或错误类型可以作为输入传递到此参数中。
返回值
此 assert 函数 fn 不会引发错误。如果抛出错误,将有两种情况 -
如果 error 类型与 error 参数相同,则 assert.doesNotThrow() 函数将向输出抛出 AssertionError。
如果未传递 error 参数或 error 类型不匹配,则传播回调用方。
例在下面的示例中,我们将创建一个函数,并在该函数内部调用 TypeError。
const assert = require('assert').strict;
function func(){
throw new TypeError('Error...');
};
assert.doesNotThrow( () => { func(); }, TypeError );
输出
在执行上述程序时,将导致 assert 错误,并显示消息“Got unwanted exception”。
assert.js:79
throw new AssertionError(obj);
^
AssertionError [ERR_ASSERTION]: Got unwanted exception.
Actual message: "Error..."
at Object.<anonymous> (/home/cg/root/639c2bf348ea8/main.js:7:8)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
throw new AssertionError(obj);
^
AssertionError [ERR_ASSERTION]: Got unwanted exception.
Actual message: "Error..."
at Object.<anonymous> (/home/cg/root/639c2bf348ea8/main.js:7:8)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
例
在下面的示例中,我们将创建一个函数,并在该函数内部调用 TypeError。但是 error 参数持有 SyntaxError 而不是 TypeError,后者属于不同的类型。
const assert = require('assert');
function func(){
throw new TypeError('Error...');
}
assert.doesNotThrow( () => { func() }, SyntaxError);
输出
在执行上述程序时,该函数将抛出 TypeError 而不是 AssertionError,因为 assert 中没有匹配的错误类型。
assert.js:604
throw actual;
^
TypeError: Error...
at func (/home/cg/root/639c2bf348ea8/main.js:4:9)
at assert.doesNotThrow (/home/cg/root/639c2bf348ea8/main.js:7:30)
at getActual (assert.js:497:5)
at Function.doesNotThrow (assert.js:616:32)
at Object.<anonymous> (/home/cg/root/639c2bf348ea8/main.js:7:8)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
throw actual;
^
TypeError: Error...
at func (/home/cg/root/639c2bf348ea8/main.js:4:9)
at assert.doesNotThrow (/home/cg/root/639c2bf348ea8/main.js:7:30)
at getActual (assert.js:497:5)
at Function.doesNotThrow (assert.js:616:32)
at Object.<anonymous> (/home/cg/root/639c2bf348ea8/main.js:7:8)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
例
在下面的示例中,我们正在创建一个函数,在函数内部,我们正在调用一个带有错误消息的 TypeError。我们还将文本传递到 message 参数中。
const assert = require('assert');
function func(){
throw new TypeError('Error...');
}
assert.doesNotThrow( () => { func() }, /Error.../, 'This is a text from MESSAGE parameter.');
输出
由于该参数也与 TypeError 中的错误消息相同,因此该函数会将 AssertionError 与消息一起抛出到输出中。
assert.js:79
throw new AssertionError(obj);
^AssertionError [ERR_ASSERTION]: Got unwanted exception: This is a text from MESSAGE parameter.
Actual message: "Error..."
at Object.<anonymous> (/home/cg/root/639c2bf348ea8/main.js:7:8)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
throw new AssertionError(obj);
^AssertionError [ERR_ASSERTION]: Got unwanted exception: This is a text from MESSAGE parameter.
Actual message: "Error..."
at Object.<anonymous> (/home/cg/root/639c2bf348ea8/main.js:7:8)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)