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])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。