mochajs/mocha · error · TypeError
Missing runner argument
Error message
Missing runner argument
What it means
The `Base` reporter constructor requires a runner instance as its first argument because it immediately reads `runner.stats`, attaches event listeners, and tracks failures. A `TypeError` is thrown when the reporter is instantiated without one, which almost always means Mocha internals were wired incorrectly.
Source
Thrown at lib/reporters/base.js:63
/**
* @abstract
* @description
* All other reporters generally inherit from this reporter.
*/
export class Base {
/**
* Constructs a new `Base` reporter instance.
*
* @public
* @memberof Mocha.reporters
* @param {Runner} runner - Instance triggers reporter actions.
* @param {Object} [options] - runner options
*/
constructor(runner, options) {
var failures = (this.failures = []);
if (!runner) {
throw new TypeError("Missing runner argument");
}
this.options = options || {};
this.runner = runner;
this.stats = runner.stats; // assigned so Reporters keep a closer reference
var maxDiffSizeOpt =
this.options.reporterOption && this.options.reporterOption.maxDiffSize;
if (maxDiffSizeOpt !== undefined && !isNaN(Number(maxDiffSizeOpt))) {
Base.maxDiffSize = Number(maxDiffSizeOpt);
}
runner.on(EVENT_TEST_PASS, function (test) {
if (test.duration > test.slow()) {
test.speed = "slow";
} else if (test.duration > test.slow() / 2) {
test.speed = "medium";
} else {
test.speed = "fast";View on GitHub (pinned to 6bcbee4fd9)
Solutions
- Always pass the runner: `new MyReporter(runner, options)`.
- In custom reporter subclasses, forward arguments to the base class: `constructor(runner, options) { super(runner, options); ... }`.
- Let Mocha instantiate reporters via `new Mocha({reporter: 'spec'}).run()` instead of constructing them yourself.
Example fix
// before
constructor(options) {
super(); // TypeError: Missing runner argument
}
// after
constructor(runner, options) {
super(runner, options);
} Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof runner === 'undefined' || runner === null) {
throw new TypeError('Cannot create reporter: runner is required');
} Type guard
const isRunner = (r) => r != null && typeof r.on === 'function' && typeof r.emit === 'function';
Try / catch
try {
const reporter = new Base(runner, options);
} catch (err) {
if (err instanceof TypeError && err.message === 'Missing runner argument') {
console.error('Reporter requires a runner from mocha.run()');
}
throw err;
} Prevention
- Always forward constructor args to super() in custom reporters.
- Construct reporters via Mocha's public API rather than by hand.
- Validate the runner exists before instantiating reporters in tooling.
- Type reporters as (runner: Mocha.Runner, options?: any) so TS catches missing args.
When it happens
Trigger: `new (require('mocha/lib/reporters/base'))()` with no runner; instantiating a custom reporter directly without passing the runner; a wrapper/adapter class forgetting to forward the runner to `super()`.
Common situations: Custom reporters calling `super()` without forwarding `runner`; test tooling that constructs reporters manually outside of `mocha.run()`; framework integrations creating a reporter before the runner exists.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- Not enough non-option arguments: got 0, need at least 1
- Not enough arguments following: ${name}
- invalid or unsupported TAP version: ${JSON.stringify(tapVers
- Missing runner argument
- Invalid argument; expected a non-empty object
AI-assisted analysis of mochajs/mocha@6bcbee4fd9 (2026-09-01).
Data as JSON: /api/errors/dafdce161a2c82f6.
Report an issue: GitHub.