assert.doesNotReject(asyncFn[, error][, message])


    等待 asyncFn promise,或者,如果 asyncFn 是函数,则立即调用该函数并等待返回的 promise 完成。 然后会检查 promise 是否没有被拒绝。

    Awaits the asyncFn promise or, if asyncFn is a function, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is not rejected.

    If asyncFn is a function and it throws an error synchronously, assert.doesNotReject() will return a rejected Promise with that error. If the function does not return a promise, assert.doesNotReject() will return a rejected Promise with an ERR_INVALID_RETURN_VALUE error. In both cases the error handler is skipped.

    Using assert.doesNotReject() is actually not useful because there is little benefit in catching a rejection and then rejecting it again. Instead, consider adding a comment next to the specific code path that should not reject and keep error messages as expressive as possible.

    If specified, error can be a Class, RegExp, or a validation function. See assert.throws() for more details.

    Besides the async nature to await the completion behaves identically to assert.doesNotThrow().

    import assert from 'node:assert/strict';
    
    await assert.doesNotReject(
      async () => {
        throw new TypeError('Wrong value');
      },
      SyntaxError
    );const assert = require('node:assert/strict');
    
    (async () => {
      await assert.doesNotReject(
        async () => {
          throw new TypeError('Wrong value');
        },
        SyntaxError
      );
    })();
    import assert from 'node:assert/strict';
    
    assert.doesNotReject(Promise.reject(new TypeError('Wrong value')))
      .then(() => {
        // ...
      });const assert = require('node:assert/strict');
    
    assert.doesNotReject(Promise.reject(new TypeError('Wrong value')))
      .then(() => {
        // ...
      });