assert.throws(fn[, error][, message])
版本 | 變化 |
---|---|
v10.2.0 |
|
v9.9.0 |
|
v4.2.0 |
|
v0.1.21 | 添加於:v0.1.21 |
參數
fn
<Function>error
<RegExp> | <Function> | <Object> | <Error>message
<string>
期望函數 fn
引發錯誤。
如果指定,error
可以是
、 Class
、驗證函數、每個屬性都將被測試以進行嚴格深度相等的驗證對象,或者是每個屬性將被測試以進行嚴格深度相等的錯誤實例包括不可枚舉的RegExp
message
和name
屬性。使用對象時,也可以在針對字符串屬性進行驗證時使用正則表達式。請參閱下麵的示例。
如果指定,如果 fn
調用失敗或錯誤驗證失敗,則 message
將附加到 AssertionError
提供的消息中。
自定義驗證對象/錯誤實例:
import assert from 'node:assert/strict';
const err = new TypeError('Wrong value');
err.code = 404;
err.foo = 'bar';
err.info = {
nested: true,
baz: 'text'
};
err.reg = /abc/i;
assert.throws(
() => {
throw err;
},
{
name: 'TypeError',
message: 'Wrong value',
info: {
nested: true,
baz: 'text'
}
// Only properties on the validation object will be tested for.
// Using nested objects requires all properties to be present. Otherwise
// the validation is going to fail.
}
);
// Using regular expressions to validate error properties:
assert.throws(
() => {
throw err;
},
{
// The `name` and `message` properties are strings and using regular
// expressions on those will match against the string. If they fail, an
// error is thrown.
name: /^TypeError$/,
message: /Wrong/,
foo: 'bar',
info: {
nested: true,
// It is not possible to use regular expressions for nested properties!
baz: 'text'
},
// The `reg` property contains a regular expression and only if the
// validation object contains an identical regular expression, it is going
// to pass.
reg: /abc/i
}
);
// Fails due to the different `message` and `name` properties:
assert.throws(
() => {
const otherErr = new Error('Not found');
// Copy all enumerable properties from `err` to `otherErr`.
for (const [key, value] of Object.entries(err)) {
otherErr[key] = value;
}
throw otherErr;
},
// The error's `message` and `name` properties will also be checked when using
// an error as validation object.
err
);
const assert = require('node:assert/strict');
const err = new TypeError('Wrong value');
err.code = 404;
err.foo = 'bar';
err.info = {
nested: true,
baz: 'text'
};
err.reg = /abc/i;
assert.throws(
() => {
throw err;
},
{
name: 'TypeError',
message: 'Wrong value',
info: {
nested: true,
baz: 'text'
}
// Only properties on the validation object will be tested for.
// Using nested objects requires all properties to be present. Otherwise
// the validation is going to fail.
}
);
// Using regular expressions to validate error properties:
assert.throws(
() => {
throw err;
},
{
// The `name` and `message` properties are strings and using regular
// expressions on those will match against the string. If they fail, an
// error is thrown.
name: /^TypeError$/,
message: /Wrong/,
foo: 'bar',
info: {
nested: true,
// It is not possible to use regular expressions for nested properties!
baz: 'text'
},
// The `reg` property contains a regular expression and only if the
// validation object contains an identical regular expression, it is going
// to pass.
reg: /abc/i
}
);
// Fails due to the different `message` and `name` properties:
assert.throws(
() => {
const otherErr = new Error('Not found');
// Copy all enumerable properties from `err` to `otherErr`.
for (const [key, value] of Object.entries(err)) {
otherErr[key] = value;
}
throw otherErr;
},
// The error's `message` and `name` properties will also be checked when using
// an error as validation object.
err
);
使用構造函數驗證 instanceof:
import assert from 'node:assert/strict';
assert.throws(
() => {
throw new Error('Wrong value');
},
Error
);
const assert = require('node:assert/strict');
assert.throws(
() => {
throw new Error('Wrong value');
},
Error
);
使用
驗證錯誤消息:RegExp
使用正則表達式在錯誤對象上運行.toString
,因此也會包含錯誤名稱。
import assert from 'node:assert/strict';
assert.throws(
() => {
throw new Error('Wrong value');
},
/^Error: Wrong value$/
);
const assert = require('node:assert/strict');
assert.throws(
() => {
throw new Error('Wrong value');
},
/^Error: Wrong value$/
);
自定義錯誤驗證:
該函數必須返回 true
以指示所有內部驗證已通過。否則它將失敗並顯示
。AssertionError
import assert from 'node:assert/strict';
assert.throws(
() => {
throw new Error('Wrong value');
},
(err) => {
assert(err instanceof Error);
assert(/value/.test(err));
// Avoid returning anything from validation functions besides `true`.
// Otherwise, it's not clear what part of the validation failed. Instead,
// throw an error about the specific validation that failed (as done in this
// example) and add as much helpful debugging information to that error as
// possible.
return true;
},
'unexpected error'
);
const assert = require('node:assert/strict');
assert.throws(
() => {
throw new Error('Wrong value');
},
(err) => {
assert(err instanceof Error);
assert(/value/.test(err));
// Avoid returning anything from validation functions besides `true`.
// Otherwise, it's not clear what part of the validation failed. Instead,
// throw an error about the specific validation that failed (as done in this
// example) and add as much helpful debugging information to that error as
// possible.
return true;
},
'unexpected error'
);
error
不能是字符串。如果提供了字符串作為第二個參數,則假定 error
被省略,而該字符串將用於 message
。這可能導致easy-to-miss 錯誤。使用與拋出的錯誤消息相同的消息將導致 ERR_AMBIGUOUS_ARGUMENT
錯誤。如果考慮使用字符串作為第二個參數,請仔細閱讀下麵的示例:
import assert from 'node:assert/strict';
function throwingFirst() {
throw new Error('First');
}
function throwingSecond() {
throw new Error('Second');
}
function notThrowing() {}
// The second argument is a string and the input function threw an Error.
// The first case will not throw as it does not match for the error message
// thrown by the input function!
assert.throws(throwingFirst, 'Second');
// In the next example the message has no benefit over the message from the
// error and since it is not clear if the user intended to actually match
// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.
assert.throws(throwingSecond, 'Second');
// TypeError [ERR_AMBIGUOUS_ARGUMENT]
// The string is only used (as message) in case the function does not throw:
assert.throws(notThrowing, 'Second');
// AssertionError [ERR_ASSERTION]: Missing expected exception: Second
// If it was intended to match for the error message do this instead:
// It does not throw because the error messages match.
assert.throws(throwingSecond, /Second$/);
// If the error message does not match, an AssertionError is thrown.
assert.throws(throwingFirst, /Second$/);
// AssertionError [ERR_ASSERTION]
const assert = require('node:assert/strict');
function throwingFirst() {
throw new Error('First');
}
function throwingSecond() {
throw new Error('Second');
}
function notThrowing() {}
// The second argument is a string and the input function threw an Error.
// The first case will not throw as it does not match for the error message
// thrown by the input function!
assert.throws(throwingFirst, 'Second');
// In the next example the message has no benefit over the message from the
// error and since it is not clear if the user intended to actually match
// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.
assert.throws(throwingSecond, 'Second');
// TypeError [ERR_AMBIGUOUS_ARGUMENT]
// The string is only used (as message) in case the function does not throw:
assert.throws(notThrowing, 'Second');
// AssertionError [ERR_ASSERTION]: Missing expected exception: Second
// If it was intended to match for the error message do this instead:
// It does not throw because the error messages match.
assert.throws(throwingSecond, /Second$/);
// If the error message does not match, an AssertionError is thrown.
assert.throws(throwingFirst, /Second$/);
// AssertionError [ERR_ASSERTION]
由於error-prone 表示法令人困惑,請避免將字符串作為第二個參數。
相關用法
- Node.js assert.throws()用法及代碼示例
- Node.js assert.notEqual(actual, expected[, message])用法及代碼示例
- Node.js assert.notDeepStrictEqual(actual, expected[, message])用法及代碼示例
- Node.js assert.fail(actual, expected[, message[, operator[, stackStartFn]]])用法及代碼示例
- Node.js assert.deepStrictEqual()用法及代碼示例
- Node.js assert.deepEqual(actual, expected[, message])用法及代碼示例
- Node.js assert.equal()用法及代碼示例
- Node.js assert.ifError()用法及代碼示例
- Node.js assert.ok()用法及代碼示例
- Node.js assert.strictEqual()用法及代碼示例
- Node.js assert.fail()用法及代碼示例
- Node.js assert.strictEqual(actual, expected[, message])用法及代碼示例
- Node.js assert.doesNotThrow(fn[, error][, message])用法及代碼示例
- Node.js assert.notStrictEqual(actual, expected[, message])用法及代碼示例
- Node.js assert.deepStrictEqual(actual, expected[, message])用法及代碼示例
- Node.js assert.match()用法及代碼示例
- Node.js assert.notDeepStrictEqual()用法及代碼示例
- Node.js assert.doesNotThrow()用法及代碼示例
- Node.js assert.rejects()用法及代碼示例
- Node.js assert.doesNotReject(asyncFn[, error][, message])用法及代碼示例
- Node.js assert.rejects(asyncFn[, error][, message])用法及代碼示例
- Node.js assert.match(string, regexp[, message])用法及代碼示例
- Node.js assert.equal(actual, expected[, message])用法及代碼示例
- Node.js assert.ifError(value)用法及代碼示例
- Node.js assert.ok(value[, message])用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 assert.throws(fn[, error][, message])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。