assert.fail(actual, expected[, message[, operator[, stackStartFn]]])
assert.fail([message]) 或其他断言函数。actual<any>expected<any>message<string> | <Error>operator<string> 默认值:'!='stackStartFn<Function> 默认值:assert.fail
如果 message 为假值,错误信息将被设置为 actual 和 expected 的值,并由提供的 operator 分隔。如果只提供了 actual 和 expected 两个参数,operator 将默认为 '!='。如果第三个参数提供了 message,它将被用作错误信息,其余参数将作为属性存储在抛出的对象上。如果提供了 stackStartFn,堆栈中该函数上方的所有堆栈帧将被移除(见 Error.captureStackTrace)。如果没有提供任何参数,将使用默认信息 Failed。
【If message is falsy, the error message is set as the values of actual and
expected separated by the provided operator. If just the two actual and
expected arguments are provided, operator will default to '!='. If
message is provided as third argument it will be used as the error message and
the other arguments will be stored as properties on the thrown object. If
stackStartFn is provided, all stack frames above that function will be
removed from stacktrace (see Error.captureStackTrace). If no arguments are
given, the default message Failed will be used.】
import assert from 'node:assert/strict';
assert.fail('a', 'b');
// AssertionError [ERR_ASSERTION]: 'a' != 'b'
assert.fail(1, 2, undefined, '>');
// AssertionError [ERR_ASSERTION]: 1 > 2
assert.fail(1, 2, 'fail');
// AssertionError [ERR_ASSERTION]: fail
assert.fail(1, 2, 'whoops', '>');
// AssertionError [ERR_ASSERTION]: whoops
assert.fail(1, 2, new TypeError('need array'));
// TypeError: need arrayconst assert = require('node:assert/strict');
assert.fail('a', 'b');
// AssertionError [ERR_ASSERTION]: 'a' != 'b'
assert.fail(1, 2, undefined, '>');
// AssertionError [ERR_ASSERTION]: 1 > 2
assert.fail(1, 2, 'fail');
// AssertionError [ERR_ASSERTION]: fail
assert.fail(1, 2, 'whoops', '>');
// AssertionError [ERR_ASSERTION]: whoops
assert.fail(1, 2, new TypeError('need array'));
// TypeError: need array在最后三种情况下,actual、expected 和 operator 对错误信息没有影响。
【In the last three cases actual, expected, and operator have no
influence on the error message.】
stackStartFn 用于截断异常堆栈跟踪的示例用法:
【Example use of stackStartFn for truncating the exception's stacktrace:】
import assert from 'node:assert/strict';
function suppressFrame() {
assert.fail('a', 'b', undefined, '!==', suppressFrame);
}
suppressFrame();
// AssertionError [ERR_ASSERTION]: 'a' !== 'b'
// at repl:1:1
// at ContextifyScript.Script.runInThisContext (vm.js:44:33)
// ...const assert = require('node:assert/strict');
function suppressFrame() {
assert.fail('a', 'b', undefined, '!==', suppressFrame);
}
suppressFrame();
// AssertionError [ERR_ASSERTION]: 'a' !== 'b'
// at repl:1:1
// at ContextifyScript.Script.runInThisContext (vm.js:44:33)
// ...