sinonjs / sinon

Test spies, stubs and mocks for JavaScript.
https://sinonjs.org/
Other
9.64k stars 772 forks source link

ThrowsPrimitive function #2524

Closed darcyrush closed 1 year ago

darcyrush commented 1 year ago

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

fatso83 commented 1 year ago

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 :)