mochajs/mocha · error · TypeError

ERR_MOCHA_INVALID_ARG_TYPE

ERR_MOCHA_INVALID_ARG_TYPE

Error message

Empty `eventName` string argument

What it means

SerializableEvent's constructor (lib/nodejs/serializer.js:139) throws ERR_MOCHA_INVALID_ARG_TYPE when the `eventName` argument is falsy (empty string, null, undefined). The event name is required because it is later re-emitted via EventEmitter#emit on the receiving side. This guards against serializing events that could not be dispatched.

Source

Thrown at lib/nodejs/serializer.js:139

export class SerializableEvent {
  /**
   * Constructs a `SerializableEvent`, throwing if we receive unexpected data.
   *
   * Practically, events emitted from `Runner` have a minimum of zero (0)
   * arguments-- (for example, {@link Runnable.constants.EVENT_RUN_BEGIN}) and a
   * maximum of two (2) (for example,
   * {@link Runnable.constants.EVENT_TEST_FAIL}, where the second argument is an
   * `Error`).  The first argument, if present, is a {@link Runnable}. This
   * constructor's arguments adhere to this convention.
   * @param {string} eventName - A non-empty event name.
   * @param {any} [originalValue] - Some data. Corresponds to extra arguments
   * passed to `EventEmitter#emit`.
   * @param {Error} [originalError] - An error, if there's an error.
   * @throws If `eventName` is empty, or `originalValue` is a non-object.
   */
  constructor(eventName, originalValue, originalError) {
    if (!eventName) {
      throw createInvalidArgumentTypeError(
        "Empty `eventName` string argument",
        "eventName",
        "string",
      );
    }
    /**
     * The event name.
     * @memberof SerializableEvent
     */
    this.eventName = eventName;
    const originalValueType = type(originalValue);
    if (originalValueType !== "object" && originalValueType !== "undefined") {
      throw createInvalidArgumentTypeError(
        `Expected object but received ${originalValueType}`,
        "originalValue",
        "object",
      );
    }

View on GitHub (pinned to 6bcbee4fd9)

Solutions

  1. Pass a non-empty string as the first argument to SerializableEvent.
  2. Validate/trim the event name variable before construction.
  3. Check the call site for undefined variables or misordered arguments.

Example fix

// before
new SerializableEvent(name, payload); // name === ''
// after
if (!name) throw new Error('event name required');
new SerializableEvent(name, payload);
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof eventName !== 'string' || eventName.length === 0) {
  throw new Error('eventName must be a non-empty string');
}

Type guard

function isValidEventName(v) {
  return typeof v === 'string' && v.length > 0;
}

Try / catch

try {
  const evt = new SerializableEvent(eventName, payload);
} catch (err) {
  if (err.code === 'ERR_MOCHA_INVALID_ARG_TYPE') {
    console.error('SerializableEvent requires a non-empty eventName string');
  } else throw err;
}

Prevention

When it happens

Trigger: Calling `new SerializableEvent('')`, `new SerializableEvent(null, value)`, or passing an event name that resolves to an empty string from a variable.

Common situations: Programmatic use of the parallel-mode serializer API; constructing events dynamically where the name variable is unset or trimmed to empty; library-internal regressions when wiring worker events.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of mochajs/mocha@6bcbee4fd9 (2026-09-01). Data as JSON: /api/errors/47ba37cf7903efa7. Report an issue: GitHub.