mochajs/mocha · error · TypeError
Invalid argument; expected a non-empty object
Error message
Invalid argument; expected a non-empty object
What it means
`utils.defineConstants(obj)` creates a frozen, prototype-less map of constants, and it only accepts a non-empty plain object. If the argument is not an object (or is an object with no own keys), it throws this TypeError as an early contract violation so callers do not silently create an empty constants map.
Source
Thrown at lib/utils.cjs:605
[Object.create(null)].concat(Array.prototype.slice.call(arguments)),
);
};
/**
* Creates a read-only map-like object.
*
* @description
* This differs from {@link module:utils.createMap createMap} only in that
* the argument must be non-empty, because the result is frozen.
*
* @see {@link module:utils.createMap createMap}
* @param {...*} [obj] - Arguments to `Object.assign()`.
* @returns {Object} A frozen object with no prototype, having `...obj` properties
* @throws {TypeError} if argument is not a non-empty object.
*/
exports.defineConstants = function (obj) {
if (canonicalType(obj) !== "object" || !Object.keys(obj).length) {
throw new TypeError("Invalid argument; expected a non-empty object");
}
return Object.freeze(exports.createMap(obj));
};
/**
* Returns current working directory
*
* Wrapper around `process.cwd()` for isolation
* @private
*/
exports.cwd = function cwd() {
return process.cwd();
};
/**
* Returns `true` if Mocha is running in a browser.
* Checks for `process.browser`.
* @returns {boolean}View on GitHub (pinned to 6bcbee4fd9)
Solutions
- Pass a non-empty plain object literal to defineConstants
- Log/inspect the argument with canonicalType and Object.keys before the call to see why it is empty or not an object
- Fix upstream code that builds the object so at least one key exists
- If constants are legitimately empty, skip the call instead of calling with {}
Example fix
// before
utils.defineConstants({});
// after
utils.defineConstants({ MOCHA_VERSION: '1.0.0' }); Defensive patterns
Strategy: validation
Validate before calling
function isNonEmptyObject(v) {
return canonicalType(v) === 'object' && Object.keys(v).length > 0;
}
if (!isNonEmptyObject(myConstants)) throw new TypeError('defineConstants needs a non-empty object');
utils.defineConstants(myConstants); Type guard
const isNonEmptyPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v) && Object.keys(v).length > 0;
Try / catch
try {
utils.defineConstants(obj);
} catch (err) {
if (!(err instanceof TypeError)) throw err;
console.error('defineConstants requires a non-empty object, got:', obj);
} Prevention
- Validate object shape and key count before calling internal utils
- Assert dynamically built constants objects are non-empty before use
- Prefer mocha's public API over internal utils
When it happens
Trigger: Calling `utils.defineConstants()` with undefined/null, with a non-object (string, array misuse), or with `{}`/`new Object()` containing no keys.
Common situations: Internal refactors where a constants object is built dynamically and ends up empty (e.g. filtering removed every key); plugin authors reaching into mocha's internal utils and passing the wrong shape; typos where a variable that should hold the object is still undefined.
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/4c16a397e12806c9.
Report an issue: GitHub.