How to use the doesNotReject function from assert

Find comprehensive JavaScript assert.doesNotReject code examples handpicked from public code repositorys.

assert.doesNotReject verifies that a Promise is not rejected, failing the test if it is rejected.

117
118
119
120
121
122
123
124
125

it('should transpile dependencies', async function() {
  const antd = porter.packet.find({ name: 'antd' });
  const style = antd.files['lib/style/default.less'];
  assert.ok(style);
  await assert.doesNotReject(async function() {
    await style.obtain();
  });
});
fork icon7
star icon40
watch icon9

+ 6 other calls in file

242
243
244
245
246
247
248
249
250
251

const entity = 'Example entity'
const domain = 'example.invalid'
addDomain(blockList, domain, entity, 'allow', rules)

await assert.doesNotReject(() =>
    generateTdsRuleset(
        blockList, supportedSurrogateScripts, '', isRegexSupportedTrue
    )
)
fork icon0
star icon0
watch icon0

+ 3 other calls in file

How does assert.doesNotReject work?

assert.doesNotReject is a function in Node.js' built-in assert module that takes in a Promise as its first argument, and an optional error message as its second argument. It returns a promise that resolves with no value if the first argument is not rejected, and rejects with an AssertionError if it is rejected. If the provided promise is rejected, assert.doesNotReject will throw an AssertionError with a message indicating that the promise was unexpectedly rejected. If an error message is provided as the second argument, it will be included in the assertion error message. If the provided promise is resolved, the returned promise will resolve with no value. If an error occurs during the evaluation of the promise, the returned promise will be rejected with that error. In summary, assert.doesNotReject allows developers to assert that a promise will not be rejected in a test or assertion scenario.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const assert = require("assert");

// A function that returns a promise that resolves after 100ms
function asyncFunction() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("Success!");
    }, 100);
  });
}

// Test that the promise resolves without an error
async function testAsyncFunction() {
  await assert.doesNotReject(asyncFunction());
  console.log("Test passed!");
}

testAsyncFunction();

In this example, assert.doesNotReject() is used to test that an asynchronous function (asyncFunction()) returns a promise that resolves successfully within 100ms. If the promise is rejected, assert.doesNotReject() will throw an error, causing the test to fail.