On this page

    M

    assert.doesNotReject

    History
    assert.doesNotReject(asyncFn, error?, message?): Promise
    Attributes
    asyncFn:Function | Promise
    message:string
    Returns: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.

    Aside from asynchronously awaiting completion, it 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(() => {
        // ...
      });