Node.js nodejs内置assert和异步/等待函数

Node.js nodejs内置assert和异步/等待函数,node.js,promise,async-await,assert,Node.js,Promise,Async Await,Assert,我使用内置的nodejsassert模块。我想断言异步函数的结果 const assert=require('assert')。严格; 异步函数f(){ 返回承诺。解决(真实); } (异步()=>{ assert.equal(等待f(),true); assert.equal(等待f(),false); })(); 如果预期值等于实际值,则它可以工作。但是如果预期的不等于实际的,我得到了未处理的PromisejectionWarning $ node example.js (node:153

我使用内置的nodejs
assert
模块。我想断言异步函数的结果

const assert=require('assert')。严格;
异步函数f(){
返回承诺。解决(真实);
}
(异步()=>{
assert.equal(等待f(),true);
assert.equal(等待f(),false);
})();
如果
预期值
等于
实际值
,则它可以工作。但是如果
预期的
不等于
实际的
,我得到了
未处理的PromisejectionWarning

$ node example.js
(node:153779) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

true !== false

    at file:///home/matt/tmp/asyncawait/example.js:9:10
(Use `node --trace-warnings ...` to show where the warning was created)
(node:153779) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:153779) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
使用选项
--未处理拒绝=严格
是否是正确的解决方案

$ node -v
v14.5.0
(更新日期:2020-07-19)

如果我设置了
--unhandled rejections=strict
,我将得到预期的输出

$ node --unhandled-rejections=strict example.js
internal/process/promises.js:213
        triggerUncaughtException(err, true /* fromPromise */);
        ^

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

true !== false

    at /home/matt/tmp/asyncawait/example.js:9:10 {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: true,
  expected: false,
  operator: 'strictEqual'
}

assert.equal
如果相等测试失败,则抛出错误。因此,如果您将
预期值
实际值
进行比较,则应使用
assert.notEqual

const assert=require('assert')。严格;
异步函数f(){
返回承诺。解决(真实);
}
(异步()=>{
assert.equal(等待f(),true);
assert.notEqual(wait f(),false);
})();

assert.equal
如果相等测试失败,将抛出错误。因此,如果您将
预期值
实际值
进行比较,则应使用
assert.notEqual

const assert=require('assert')。严格;
异步函数f(){
返回承诺。解决(真实);
}
(异步()=>{
assert.equal(等待f(),true);
assert.notEqual(wait f(),false);
})();

您需要捕获它(try/catch),您想要实现什么?看起来像是使用异步iLife允许使用
wait
的结果。尝试使用
f()执行断言。然后(…)
语法且无生命。如果您仍然收到警告,那么chain
.catch()
。您需要捕获它(try/catch),您试图实现什么?看起来像是使用异步生命来允许使用
wait
的结果。尝试使用
f()执行断言。然后(…)
语法且无生命。如果仍然收到警告,那么如果相等测试失败,chain
.catch()
>assert.equal将抛出一个错误。谢谢我得到了它。在这种情况下,我将自己处理错误。>assert.equal在相等测试失败时抛出错误。谢谢我得到了它。在这种情况下,我将自己处理错误。