MCPcopy Index your code
hub / github.com/nodejs/node / expectedException

Function expectedException

lib/assert.js:469–575  ·  view source on GitHub ↗
(actual, expected, message, fn)

Source from the content-addressed store, hash-verified

467}
468
469function expectedException(actual, expected, message, fn) {
470 let generatedMessage = false;
471 let throwError = false;
472
473 if (typeof expected !== 'function') {
474 // Handle regular expressions.
475 if (isRegExp(expected)) {
476 const str = String(actual);
477 if (RegExpPrototypeExec(expected, str) !== null)
478 return;
479
480 if (!message) {
481 generatedMessage = true;
482 message = 'The input did not match the regular expression ' +
483 `${inspect(expected)}. Input:\n\n${inspect(str)}\n`;
484 }
485 throwError = true;
486 // Handle primitives properly.
487 } else if (typeof actual !== 'object' || actual === null) {
488 const err = new AssertionError({
489 actual,
490 expected,
491 message,
492 operator: 'deepStrictEqual',
493 stackStartFn: fn,
494 diff: this?.[kOptions]?.diff,
495 });
496 err.operator = fn.name;
497 throw err;
498 } else {
499 // Handle validation objects.
500 const keys = ObjectKeys(expected);
501 // Special handle errors to make sure the name and the message are
502 // compared as well.
503 if (expected instanceof Error) {
504 ArrayPrototypePush(keys, 'name', 'message');
505 } else if (keys.length === 0) {
506 throw new ERR_INVALID_ARG_VALUE('error',
507 expected, 'may not be an empty object');
508 }
509 if (isDeepEqual === undefined) lazyLoadComparison();
510 for (const key of keys) {
511 if (typeof actual[key] === 'string' &&
512 isRegExp(expected[key]) &&
513 RegExpPrototypeExec(expected[key], actual[key]) !== null) {
514 continue;
515 }
516 compareExceptionKey(actual, expected, key, message, keys, fn);
517 }
518 return;
519 }
520 // Guard instanceof against arrow functions as they don't have a prototype.
521 // Check for matching Error classes.
522 } else if (expected.prototype !== undefined && actual instanceof expected) {
523 return;
524 } else if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
525 if (!message) {
526 generatedMessage = true;

Callers

nothing calls this directly

Calls 5

StringClass · 0.85
lazyLoadComparisonFunction · 0.85
compareExceptionKeyFunction · 0.85
inspectFunction · 0.50
isErrorFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…