sinonjs / sinon

Test spies, stubs and mocks for JavaScript.

Home Page:https://sinonjs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ThrowsPrimitive function

darcyrush opened this issue · comments

Is your feature request related to a problem? Please describe.
Javascript has some warts where throw 123 or throw 'string' is completely legal, and recent TypeScript hints at this with Error cause types being defined as unknown

Currently stub().throws(123) throws a number, but stub().throws('string') throws an Error object named 'string'.

Describe the solution you'd like
A function to throw a non object (string)

Describe alternatives you've considered
Personally I prefer and use the more explicit version of stub().throws(new Error(string)) but its not feasible to change stub().throws('string') to actually throw a string due to backwards compatibility, so I think a new function would be beneficial

Thinking about what you actually want to achieve with this, a function that throws a primitive, you actually already have this today:

const s = sinon.stub().throws(() => 42)
s();
> Uncaught 42

So I do not see the need to expand the API further :)