{"record":{"id":"9b71054c0772dd44","repo":"mochajs/mocha","slug":"err-mocha-invalid-arg-type-9b7105","errorCode":"ERR_MOCHA_INVALID_ARG_TYPE","errorMessage":"Test argument \"title\" should be a string. Received type \"${typeof title}\"","messagePattern":"Test argument \"title\" should be a string\\. Received type \"(.+?)\"","errorType":"error_code","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/test.js","lineNumber":19,"sourceCode":"import { Runnable } from \"./runnable.js\";\nimport utils from \"./utils.cjs\";\nimport { createInvalidArgumentTypeError } from \"./errors.js\";\n\nconst { isString } = utils;\nconst { MOCHA_ID_PROP_NAME } = utils.constants;\n\nclass Test extends Runnable {\n  /**\n   * Initialize a new `Test` with the given `title` and callback `fn`.\n   *\n   * @public\n   * @extends Runnable\n   * @param {String} title - Test title (required)\n   * @param {Function} [fn] - Test callback.  If omitted, the Test is considered \"pending\"\n   */\n  constructor(title, fn) {\n    if (!isString(title)) {\n      throw createInvalidArgumentTypeError(\n        'Test argument \"title\" should be a string. Received type \"' +\n          typeof title +\n          '\"',\n        \"title\",\n        \"string\",\n      );\n    }\n    super(title, fn);\n    this.type = \"test\";\n    this.reset();\n  }\n\n  /**\n   * Resets the state initially or for a next run.\n   */\n  reset() {\n    super.reset();\n    this.pending = !this.fn;","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/mochajs/mocha/blob/6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a/lib/test.js#L1-L37","documentation":"The Test constructor requires its first argument `title` to be a string; any other type throws ERR_MOCHA_INVALID_ARG_TYPE. This is an eager input validation so misconstructed tests fail loudly at construction rather than producing cryptic errors later during the run.","triggerScenarios":"Calling new Test(123), new Test(undefined, fn) (e.g. from a helper generating tests), or passing a non-string title programmatically — including Test.extend or it()/specify() wrappers that forward titles.","commonSituations":"Dynamically generating tests from data where a key is undefined/null; array index used as title (number); template helper returning a non-string; copying example code that passed an object.","solutions":["Pass a string title, e.g. new Test(String(name), fn) or `new Test(\\`case ${i}\\`, fn)`.","Validate/coerce dynamic titles before constructing: skip entries with missing titles or stringify them.","Check helpers/wrappers around `it()` that may drop or misplace the title argument.","For data-driven tests, filter out records whose label is not a string before mapping to Tests."],"exampleFix":"// before\nitems.forEach((item, i) => it(item.name, () => run(item))); // item.name undefined\n// after\nitems.filter((item) => typeof item.name === 'string').forEach((item) => it(item.name, () => run(item)));","handlingStrategy":"type-guard","validationCode":"function safeTest(title, fn) {\n  if (typeof title !== 'string') throw new TypeError(`Test title must be a string, got ${typeof title}`);\n  return new Test(title, fn);\n}","typeGuard":"function isTestTitle(v) {\n  return typeof v === 'string';\n}\n// usage: items.filter(i => isTestTitle(i.name)).forEach(i => it(i.name, ...))","tryCatchPattern":"try {\n  tests.push(new Test(rawTitle, fn));\n} catch (err) {\n  if (err.code === 'ERR_MOCHA_INVALID_ARG_TYPE') {\n    console.error(`Skipping test with invalid title: ${String(rawTitle)}`);\n  }\n}","preventionTips":["Coerce dynamic titles with String(...) or template literals","Filter data-driven test inputs for missing/undefined labels","Never pass numbers/objects as test titles","Wrap test-generation helpers with title validation"],"tags":["arguments","type-error","test-constructor"],"backgroundTag":"invalid-argument-value","analyzedSha":"6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a","analyzedAt":"2026-09-01T03:41:47.182Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}