{"id":"3ced69e6e525414d","repo":"jestjs/jest","slug":"this-matcher-must-not-have-an-expected-argument","errorCode":null,"errorMessage":"this matcher must not have an expected argument","messagePattern":"this matcher must not have an expected argument","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-matcher-utils/src/index.ts","lineNumber":163,"sourceCode":"): string {\n  const type = getType(value);\n  const hasType =\n    type !== 'null' && type !== 'undefined'\n      ? `${name} has type:  ${type}\\n`\n      : '';\n  const hasValue = `${name} has value: ${print(value)}`;\n  return hasType + hasValue;\n}\n\nexport const ensureNoExpected = (\n  expected: unknown,\n  matcherName: string,\n  options?: MatcherHintOptions,\n): void => {\n  if (expected !== undefined) {\n    // Prepend maybe not only for backward compatibility.\n    const matcherString = (options ? '' : '[.not]') + matcherName;\n    throw new Error(\n      matcherErrorMessage(\n        matcherHint(matcherString, undefined, '', options),\n        // Because expected is omitted in hint above,\n        // expected is black instead of green in message below.\n        'this matcher must not have an expected argument',\n        printWithType('Expected', expected, printExpected),\n      ),\n    );\n  }\n};\n\n/**\n * Ensures that `actual` is of type `number | bigint`\n */\nexport const ensureActualIsNumber = (\n  actual: unknown,\n  matcherName: string,\n  options?: MatcherHintOptions,","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-matcher-utils/src/index.ts#L145-L181","documentation":"Thrown by `ensureNoExpected` in jest-matcher-utils/index.ts:163 (via `matcherErrorMessage`) when `expected !== undefined`. This helper backs zero-argument matchers such as `toBeUndefined()`, `toBeDefined()`, `toBeNull()`, `toBeTruthy()`, `toBeFalsy()`, `toHaveBeenCalled()` — they take no expected value, so passing one is a usage error.","triggerScenarios":"Writing `expect(x).toBeUndefined(null)`, `expect(fn).toHaveBeenCalled(2)`, or `expect(x).toBeTruthy('yes')`. Also hit by custom matchers that call `ensureNoExpected(expected, matcherName)` but receive an unexpected second argument.","commonSituations":"Confusing `toBeUndefined()` with `toBe(undefined)`; calling `toHaveBeenCalled()` with a count (use `toHaveBeenCalledTimes(n)` instead); custom matcher authored without checking argument count.","solutions":["Drop the argument: `expect(x).toBeUndefined()`.","Use the right matcher: `toHaveBeenCalledTimes(n)` for counts, `toBe(undefined)` if you need a value comparison.","For custom matchers, call `ensureNoExpected` only in the no-arg branch and validate `expectedArgument` length yourself."],"exampleFix":"// before\nexpect(value).toBeUndefined(null)\n// after\nexpect(value).toBeUndefined()","handlingStrategy":"validation","validationCode":"// zero-argument matchers: do not pass a second arg\nexpect(value).toBeUndefined();\n// if a value is required, use the value-comparing matcher:\nexpect(value).toBe(undefined);","typeGuard":"const isZeroArgMatcher = (name: string): boolean =>\n  ['toBeUndefined','toBeDefined','toBeNull','toBeTruthy','toBeFalsy','toHaveBeenCalled'].includes(name);","tryCatchPattern":null,"preventionTips":["Learn which matchers are zero-argument (toBeUndefined, toBeDefined, toBeNull, toBeTruthy, toBeFalsy, toHaveBeenCalled).","For call counts use toHaveBeenCalledTimes(n), not toHaveBeenCalled(n)."],"tags":["matcher-utils","ensure-no-expected","matcher-error","zero-argument-matcher"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}