How to use the doesNotThrow function from assert

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

assert.doesNotThrow method in Node.js is used to check that a given function does not throw any error when executed.

146
147
148
149
150
151
152
153
154
  assert.equal(cjs.includes('lib'), false);
  assert.equal(cjs.length, 0);
});

it('does not throw on unparsable .js files', () => {
  assert.doesNotThrow(() => {
    precinct(read('unparseable.js'));
  }, SyntaxError);
});
fork icon39
star icon177
watch icon0

+ 3 other calls in file

530
531
532
533
534
535
536
537
538
});

it('should ensure maximum message length for a 1025-bit key is not exceeded', function() {
  var key = PKI.publicKeyFromPem(tests[1].publicKeyPem);
  var message = UTIL.createBuffer().fillWithByte(0, 118);
  ASSERT.doesNotThrow(function() {
    key.encrypt(message.getBytes());
  });
});
fork icon0
star icon0
watch icon1

How does assert.doesNotThrow work?

The assert.doesNotThrow() method is used to test if a function does not throw any errors or exceptions when called, and if it does, the test fails with an AssertionError. It accepts a function as its first argument and an optional error message as its second argument. The function is called by assert.doesNotThrow() and if it does not throw any errors, the test passes. If it does throw an error, the test fails and the error is passed to the AssertionError. If an error message is provided as the second argument, it will be included in the AssertionError.

77
78
79
80
81
82
83
84
85
86
  mpath.get(Math, o);
}, /Must be either string or array/);
assert.throws(function() {
  mpath.get(Buffer, o);
}, /Must be either string or array/);
assert.doesNotThrow(function() {
  mpath.get('string', o);
});
assert.doesNotThrow(function() {
  mpath.get([], o);
fork icon0
star icon0
watch icon1

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function divide(a, b) {
  if (b === 0) {
    throw new Error("Division by zero");
  }
  return a / b;
}

// Test that divide() does not throw an error when dividing by a non-zero number
assert.doesNotThrow(() => {
  divide(4, 2);
});

// Test that divide() throws an error when dividing by zero
assert.throws(
  () => {
    divide(4, 0);
  },
  Error,
  "Division by zero"
);

In this example, assert.doesNotThrow() is used to test that the divide() function does not throw an error when dividing by a non-zero number. The first argument to assert.doesNotThrow() is a function that is expected to not throw an error, and the second argument is an optional error message to display if the function does throw an error.