{"id":"9f18fdcbf8f631ae","repo":"vitest-dev/vitest","slug":"cannot-use-shorthand-when-called-with-new","errorCode":null,"errorMessage":"Cannot use `${shorthand}` when called with `new`. Use `mockImplementation` with a `class` keyword instead. See https://vitest.dev/api/mock#class-support for more information.","messagePattern":"Cannot use `(.+?)` when called with `new`\\. Use `mockImplementation` with a `class` keyword instead\\. See https://vitest\\.dev/api/mock#class-support for more information\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/spy/src/index.ts","lineNumber":716,"sourceCode":"}\n\nexport function restoreAllMocks(): void {\n  for (const restore of MOCK_RESTORE) {\n    restore()\n  }\n  MOCK_RESTORE.clear()\n}\n\nexport function clearAllMocks(): void {\n  REGISTERED_MOCKS.forEach(mock => mock.mockClear())\n}\n\nexport function resetAllMocks(): void {\n  REGISTERED_MOCKS.forEach(mock => mock.mockReset())\n}\n\nfunction throwConstructorError(shorthand: string): never {\n  throw new TypeError(\n    `Cannot use \\`${shorthand}\\` when called with \\`new\\`. Use \\`mockImplementation\\` with a \\`class\\` keyword instead. See https://vitest.dev/api/mock#class-support for more information.`,\n  )\n}\n\nexport type {\n  Constructable,\n  MaybeMocked,\n  MaybeMockedConstructor,\n  MaybeMockedDeep,\n  MaybePartiallyMocked,\n  MaybePartiallyMockedDeep,\n  Mock,\n  MockContext,\n  Mocked,\n  MockedClass,\n  MockedFunction,\n  MockedFunctionDeep,\n  MockedObject,","sourceCodeStart":698,"sourceCodeEnd":734,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/spy/src/index.ts#L698-L734","documentation":"Thrown by throwConstructorError() in packages/spy/src/index.ts when a mock configured with a shorthand return-style method (mockReturnValue, mockReturnValueOnce, mockResolvedValue, mockResolvedValueOnce, mockRejectedValue, mockRejectedValueOnce) is invoked with new. The shorthand implementations return a value, which is meaningless as a constructor result; Vitest requires an explicit mockImplementation(function/class){...}) so the mock can be instantiated. The guard fires inside the implementation via new.target.","triggerScenarios":"const Mock = vi.fn(); Mock.mockReturnValue({}); new Mock(); — i.e. constructing an instance from a mock whose implementation was set with a 'return value' shorthand.","commonSituations":"Mocking a class but using mockReturnValue to supply the instance; refactoring a function into a class without updating the mock setup; copy-pasting a function mock pattern onto a constructor.","solutions":["Replace the shorthand with mockImplementation using a function or class: Mock.mockImplementation(function (...) { return {...} }).","Use vi.fn(class { ... }) if you need a real constructable mock.","Avoid new on mocks set up with mockResolvedValue/mockRejectedValue — those only make sense for function calls returning promises.","If you need both call and construct behavior, provide a mockImplementation that branches on new.target."],"exampleFix":"// before\nconst Mock = vi.fn()\nMock.mockReturnValue({ id: 1 })\nnew Mock()\n\n// after\nconst Mock = vi.fn()\nMock.mockImplementation(function () { return { id: 1 } })\nnew Mock()","handlingStrategy":"type-guard","validationCode":"function isConstructableShorthand(setup: unknown): boolean {\n  // detect shorthand-return configurations that cannot be used with `new`\n  return typeof setup === 'function'\n}","typeGuard":"const looksLikeClass = (v: unknown): boolean =>\n  typeof v === 'function' && /^class\\s/.test(Function.prototype.toString.call(v))","tryCatchPattern":null,"preventionTips":["Never pair mockReturnValue/mockResolvedValue/mockRejectedValue with new Mock().","Use mockImplementation(function|class) when the mock must be instantiated.","Branch on new.target in mockImplementation if a single mock must support both call and construct."],"tags":["spy","mock","constructor","api-misuse"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}