w3resource

Understanding Mocha Error Codes


In this tutorial we will be examining the error codes you can get while running a mocha test.

This feature is new in V6.0.0. when Mocha throws an error by itself, the associated Error has a code property. Where it is applicable, the consumers have to check the code property rather than string-matching against the message property. Below is a table that describes each of the erro codes.

Code Description
ERR_MOCHA_INVALID_ARG_TYPE This signifies that the wrong type was passed for a given argument.
ERR_MOCHA_INVALID_ARG_VALUE This signifies that an invalid or unsupported value was passed for a given argument.
ERR_MOCHA_INVALID_EXCEPTION This signifies that a falsy or otherwise underspecified exception was thrown.
ERR_MOCHA_INVALID_INTERFACE This signifies that the interface specified in options is  not found.
ERR_MOCHA_INVALID_REPORTER This means that the reporter specified in options is not found.
ERR_MOCHA_NO_FILES_MATCH_PATTERN The test file(s) could not be found
ERR_MOCHA_UNSUPPORTED The requested behavior, option, or parameter is not supported.
'use strict';
/**
 * @module Errors
 */
/**
 * Factory functions that creates throwable error objects
 */

will create an error object to be thrown when no files to be tested could be found using specified pattern.

function createNoFilesMatchPatternError(message, pattern) {
  var err = new Error(message);
  err.code = 'ERR_MOCHA_NO_FILES_MATCH_PATTERN';
  err.pattern = pattern;
  return err;
}

will create an error object to be thrown when the reporter specified in the options was not found.

function createInvalidReporterError(message, reporter) {
  var err = new TypeError(message);
  err.code = 'ERR_MOCHA_INVALID_REPORTER';
  err.reporter = reporter;
  return err;
}

will create an error object to be thrown when the interface specified in the options was not found.

function createInvalidInterfaceError(message, ui) {
  var err = new Error(message);
  err.code = 'ERR_MOCHA_INVALID_INTERFACE';
  err.interface = ui;
  return err;
}

will create an error object to be thrown when a behavior, option, or parameter is unsupported.

function createUnsupportedError(message) {
  var err = new Error(message);
  err.code = 'ERR_MOCHA_UNSUPPORTED';
  return err;
}

will create an error object to be thrown when an argument is missing.

function createMissingArgumentError(message, argument, expected) {
  return createInvalidArgumentTypeError(message, argument, expected);
}

will create an error object to be thrown when an argument did not use the supported type

function createInvalidArgumentTypeError(message, argument, expected) {
  var err = new TypeError(message);
  err.code = 'ERR_MOCHA_INVALID_ARG_TYPE';
  err.argument = argument;
  err.expected = expected;
  err.actual = typeof argument;
  return err;
}

will create an error object to be thrown when an argument did not use the supported value

function createInvalidArgumentValueError(message, argument, value, reason) {
  var err = new TypeError(message);
  err.code = 'ERR_MOCHA_INVALID_ARG_VALUE';
  err.argument = argument;
  err.value = value;
  err.reason = typeof reason !== 'undefined' ? reason : 'is invalid';
  return err;
}

will create an error object to be thrown when an exception was caught, but the `Error` is falsy or undefined.

function createInvalidExceptionError(message, value) {
  var err = new Error(message);
  err.code = 'ERR_MOCHA_INVALID_EXCEPTION';
  err.valueType = typeof value;
  err.value = value;
  return err;
}
module.exports = {
  createInvalidArgumentTypeError: createInvalidArgumentTypeError,
  createInvalidArgumentValueError: createInvalidArgumentValueError,
  createInvalidExceptionError: createInvalidExceptionError,
  createInvalidInterfaceError: createInvalidInterfaceError,
  createInvalidReporterError: createInvalidReporterError,
  createMissingArgumentError: createMissingArgumentError,
  createNoFilesMatchPatternError: createNoFilesMatchPatternError,
  createUnsupportedError: createUnsupportedError
};


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mocha/error-codes.php