Sinon: stub().throws("TypeError") doesn't actually throw TypeError instance when called

Created on 13 Nov 2015  路  8Comments  路  Source: sinonjs/sinon

This isn't the behavior I expected:

$ node
> var stub = require("sinon").stub().throws("TypeError")
undefined
> try { stub() } catch (e) { e instanceof TypeError }
false

I expected the type to actually be a TypeError instance, not just a renamed Error.

> try { stub() } catch (e) { e.constructor }
{ [Function: Error]
  captureStackTrace: [Function: captureStackTrace],
  stackTraceLimit: 10 }

Could this be fixed?

stale

Most helpful comment

Good catch. As far as I can tell, this has never worked the way the documentation says. However the name of the error is the value that you passed in.

e.name === 'TypeError'

I'll look into fixing this. In the mean time, you could always pass the exact error you want returned to throws as well.

var error = new TypeError('Insert message');
error.foo = 'bar';

// ...

stub.throws(error);

All 8 comments

Good catch. As far as I can tell, this has never worked the way the documentation says. However the name of the error is the value that you passed in.

e.name === 'TypeError'

I'll look into fixing this. In the mean time, you could always pass the exact error you want returned to throws as well.

var error = new TypeError('Insert message');
error.foo = 'bar';

// ...

stub.throws(error);

Hm. Shouldn't the api be used like this instead?

var stub = require("sinon").stub().throws(new TypeError());

The above throws the string "TypeError" which is absolutely possible in JavaScript. If we changed that string to something else magically, I guess we'd break a valid use case, right?

No, base on the code in the OP, it throws an instance of Error with the name set to TypeError .

  function throwsException(error, message) {
    if (typeof error === "string") {
        this.exception = new Error(message || "");
        this.exception.name = error;
    } else if (!error) {
        this.exception = new Error("Error");
    } else {
        this.exception = error;
    }

    return this;
}

source

I would be willing to take a breaking change (function to new) here to rectify this. It makes little sense why that wasn't already done in the first place, and if you're throwing a function, you probably aren't throwing the right thing (even throwing plain strings is better).

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I noticed that the documentation still says

stub.throws("TypeError");
Causes the stub to throw an exception of the provided type.

However, the above described functionality still holds true.
It is necessary to use stub.throws(new FooError()) to obtain an error that will match error instanceof FooError

@MichaelPaulukonis if you supply a small PR with a correct description we no longer have an issue ;-)

Thanks, @fatso83 - PR#1730 submitted!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zimtsui picture zimtsui  路  3Comments

OscarF picture OscarF  路  4Comments

ndhoule picture ndhoule  路  4Comments

andys8 picture andys8  路  4Comments

fearphage picture fearphage  路  3Comments