Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with restoring a getter #995

Closed
fatfisz opened this issue Feb 27, 2016 · 3 comments
Closed

Problems with restoring a getter #995

fatfisz opened this issue Feb 27, 2016 · 3 comments

Comments

@fatfisz
Copy link

fatfisz commented Feb 27, 2016

Hi I'm currently using SinonJS (v. 1.17.3 on Node.js 4.x) and I'm experiencing a small problem while restoring a mocked getter. There's this little code in the restore method:

if (object[property] === method) {
    object[property] = wrappedMethod;
}

This can sometimes cause problems if object[property] is a getter. I suggest the check is only performed if Object.defineProperty wasn't called before, as at this point we are sure that the property has been restored and the check is unnecessary.

Because the authors of SinonJS aren't very fond of mocking getters/setters, let me provide you with a bit of a background.

Here's a class that contains the getter I'm mocking:

const privateDataMap = new WeakMap();

export default class DataStore {
  constructor(input) {
    privateDataMap.set(this, {
      errors: {},
      ...
    });
  }

  ...

  get errors() {
    return privateDataMap.get(this).errors;
  }
}

I'm using this class inside some function that I'm testing and I wanted to check if the errors are passed correctly. Because I can't manipulate the object itself, I turned to the prototype, and so I've written:

const stub = sinon.stub(DataStore.prototype, 'errors', {
  get() {
    return 'test errors';
  },
});

Later on I want to clean up, and so I call stub.restore(). This obviously causes an error, because at the point object[property] is called the getter has been restored, and so the call really looks like this: privateDataMap.get(DataStore.prototype).errors;.
Because I can't access privateDataMap from the outside (that's the point), I can't set the value for privateDataMap.get(DataStore.prototype), and so a type error occurs.

@fatfisz
Copy link
Author

fatfisz commented Feb 29, 2016

Sorry, didn't notice that this is a duplicate of #897. For when is the release of 2.0.0 planned? :)

@fatfisz fatfisz closed this as completed Feb 29, 2016
@fatso83
Copy link
Contributor

fatso83 commented Feb 29, 2016

Last year - feel free to push it forwards :-) No seriously, it's getting there a lot faster nowadays with the help we have been getting on #966 from (amongst others) @fearphage and @jonnyreeves , but there is still quite a bit left.

You can get the next pre-release by doing npm install sinon@next

@fatfisz
Copy link
Author

fatfisz commented Feb 29, 2016

I've already installed the pre-release and it's working without trouble. Good work! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants