{"record":{"id":"b75ac99a3d9ea196","repo":"mochajs/mocha","slug":"missing-runner-argument-b75ac9","errorCode":null,"errorMessage":"Missing runner argument","messagePattern":"Missing runner argument","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/stats-collector.js","lineNumber":43,"sourceCode":" *\n * @private\n * @param {Runner} runner - Runner instance\n * @throws {TypeError} If falsy `runner`\n */\nfunction createStatsCollector(runner) {\n  /**\n   * @type {StatsCollector}\n   */\n  var stats = {\n    suites: 0,\n    tests: 0,\n    passes: 0,\n    pending: 0,\n    failures: 0,\n  };\n\n  if (!runner) {\n    throw new TypeError(\"Missing runner argument\");\n  }\n\n  runner.stats = stats;\n\n  runner.once(EVENT_RUN_BEGIN, function () {\n    stats.start = new Date();\n  });\n  runner.on(EVENT_SUITE_BEGIN, function (suite) {\n    suite.root || stats.suites++;\n  });\n  runner.on(EVENT_TEST_PASS, function () {\n    stats.passes++;\n  });\n  runner.on(EVENT_TEST_FAIL, function () {\n    stats.failures++;\n  });\n  runner.on(EVENT_TEST_PENDING, function () {\n    stats.pending++;","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/mochajs/mocha/blob/6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a/lib/stats-collector.js#L25-L61","documentation":"`createStatsCollector` attaches a stats object and event listeners (RUN_BEGIN/RUN_END/PASS/FAIL etc.) to a runner instance, so it requires the runner as its argument. A `TypeError` is thrown when called with undefined/null, indicating the runner was never created or wasn't passed through.","triggerScenarios":"`createStatsCollector()` with no args; `createStatsCollector(null)`; integration code that builds a fake runner but forgets to pass it; calling the collector before `mocha.run()` creates the Runner.","commonSituations":"Custom reporter/tooling setups that wire Mocha internals manually; test harness wrappers calling `createStatsCollector` without the runner from `mocha.run()`; refactors that drop the argument when the runner is created lazily.","solutions":["Pass the actual runner instance: `createStatsCollector(runner)`.","Only invoke after a Runner exists — e.g. inside/after `mocha.run(cb)` or `new Runner(suite)`.","If building custom runners, ensure it's a real EventEmitter-like object with the expected Mocha events."],"exampleFix":"// before\nconst runner = new Runner(suite);\ncreateStatsCollector(); // TypeError\n\n// after\nconst runner = new Runner(suite);\ncreateStatsCollector(runner);","handlingStrategy":"type-guard","validationCode":"if (!runner) {\n  throw new Error('createStatsCollector requires a runner instance');\n}","typeGuard":"const looksLikeRunner = (r) => r != null && typeof r.once === 'function' && typeof r.on === 'function' && typeof r.emit === 'function';","tryCatchPattern":"try {\n  createStatsCollector(runner);\n} catch (err) {\n  if (err instanceof TypeError && err.message === 'Missing runner argument') {\n    console.error('Pass the Runner instance from mocha.run() to createStatsCollector.');\n  }\n  throw err;\n}","preventionTips":["Create the Runner via mocha.run() and pass it through immediately.","In wrappers, thread the runner argument explicitly instead of relying on closures.","Never call stats collection before the runner exists.","Add an assertion in tooling that the runner is an EventEmitter before wiring stats."],"tags":["stats","runner","argument-validation"],"backgroundTag":"missing-required-argument","analyzedSha":"6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a","analyzedAt":"2026-09-01T03:41:47.182Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}