new assert.AssertionError(options)
添加于:v0.1.21
参数
options<Object>message<string> 如果提供,则错误消息设置为此值。actual<any> 错误实例上的actual属性。expected<any> 错误实例上的expected属性。operator<string> 错误实例上的operator属性。stackStartFn<Function> 如果提供,生成的堆栈跟踪将忽略此函数之前的帧。
Error 的子类,指示断言失败。
所有实例都包含内置的 Error 属性( message 和 name )和:
actual<any> 为等方法设置为assert.strictEqual()actual参数。expected<any> 为等方法设置为assert.strictEqual()expected值。generatedMessage<boolean> 指示消息是否是自动生成的(true)。code<string> 值始终为ERR_ASSERTION以表明错误是断言错误。operator<string> 设置为传入的运算符值。
import assert from 'node:assert'; // Generate an AssertionError to compare the error message later: const { message } = new assert.AssertionError({ actual: 1, expected: 2, operator: 'strictEqual' }); // Verify error output: try { assert.strictEqual(1, 2); } catch (err) { assert(err instanceof assert.AssertionError); assert.strictEqual(err.message, message); assert.strictEqual(err.name, 'AssertionError'); assert.strictEqual(err.actual, 1); assert.strictEqual(err.expected, 2); assert.strictEqual(err.code, 'ERR_ASSERTION'); assert.strictEqual(err.operator, 'strictEqual'); assert.strictEqual(err.generatedMessage, true); }const assert = require('node:assert'); // Generate an AssertionError to compare the error message later: const { message } = new assert.AssertionError({ actual: 1, expected: 2, operator: 'strictEqual' }); // Verify error output: try { assert.strictEqual(1, 2); } catch (err) { assert(err instanceof assert.AssertionError); assert.strictEqual(err.message, message); assert.strictEqual(err.name, 'AssertionError'); assert.strictEqual(err.actual, 1); assert.strictEqual(err.expected, 2); assert.strictEqual(err.code, 'ERR_ASSERTION'); assert.strictEqual(err.operator, 'strictEqual'); assert.strictEqual(err.generatedMessage, true); }
相关用法
- Node.js new assert.CallTracker()用法及代码示例
- Node.js new AsyncResource(type[, options])用法及代码示例
- Node.js new stream.Duplex(options)用法及代码示例
- Node.js new stream.Readable([options])用法及代码示例
- Node.js new Console(options)用法及代码示例
- Node.js new URLSearchParams(obj)用法及代码示例
- Node.js new crypto.Certificate()用法及代码示例
- Node.js new stream.Writable([options])用法及代码示例
- Node.js new URLSearchParams(iterable)用法及代码示例
- Node.js new Agent([options])用法及代码示例
- Node.js new vm.SourceTextModule(code[, options])用法及代码示例
- Node.js new stream.Transform([options])用法及代码示例
- Node.js new PerformanceObserver(callback)用法及代码示例
- Node.js new URL(input[, base])用法及代码示例
- Node.js new URLSearchParams(string)用法及代码示例
- Node.js net.isIP(input)用法及代码示例
- Node.js net.createConnection(options[, connectListener])用法及代码示例
- Node.js net.isIPv6(input)用法及代码示例
- Node.js net.Server.address()用法及代码示例
- Node.js net.createServer([options][, connectionListener])用法及代码示例
- Node.js net.Server.listen()用法及代码示例
- Node.js net.Socket.setTimeout(timeout[, callback])用法及代码示例
- Node.js net.BlockList.check(address[, type])用法及代码示例
- Node.js net.isIPv4(input)用法及代码示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 new assert.AssertionError(options)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
