简体   繁体   中英

SinonJS stub getter defined with configurable false

I'm trying to stub nodeJS auth0 lib ManagementClient.prototype.createUser using SinonJS .

The problem is that createUser is a getter defined with Object.defineProperty using configurable: false hence using sinon.replaceGetter() or sinon.stub(object, 'method').get(fn) throws Cannot redeclare property . I opened an issue about it on their Github's repo and someone told me that sinon.stub(ManagementClient.prototype, 'createUser', fn) was working but the problem is that this syntax is deprecated since v3.0.0 . I tried using sinon.stub(ManagementClient.prototype, 'createUser').callsFake(fn) which is supposed to replace the old syntax but it doesn't work as the previous syntax seemed to. Here is a runkit showing that in action.

My question is: is there a way for me to stub that getter even though it is declared as configurable: false or am I screwed? I can't seem to find a way.

I already asked them if they would consider updating their Object.defineProperty call to set configurable: true but to no avail.

It doesn't seem to be possible to override the property defined with configuration: false so I ended up making my own wrapper around Auth0 SDK and stubbed it instead of the SDK itself.

Here is an example:

If you have the SDK with a foo() method. You then create a new class:

import Auth0 from 'auth0';

class Auth0Wrapper {
  foo() {
    return Auth0.foo();
  }
}

This class is really dumb it acts as a proxy to the original method. The point is just that now you can easily stub your class no matter how the underlying SDK was developed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM