sandbox.restore() does not work on static members of a function or class
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 9.8k
- Forks
- 809
- PR merge metrics
- No merged PRs in 30d
Description
Describe the bug
The documentation says, that we can stub all methods of a object by using sinon.stub(obj)
https://sinonjs.org/releases/latest/stubs/#var-stub--sinonstubobj
This works great for objects and also for classes.
It also works for sandbox.stub(obj)
What doesn't work, is to restore the methods of a class by using sandbox.restore()
To Reproduce
This block works:
const sinon = require('sinon');
const sandbox = sinon.createSandbox();
console.log('stub(obj)')
let i = 1;
const myTest = {
test: (value) => {
return value;
}
}
console.log(myTest.test(i++));
sandbox.stub(myTest);
console.log(myTest.test(i++));
sandbox.restore();
console.log(myTest.test(i++));
sandbox.stub(myTest);
console.log(myTest.test(i++));
sandbox.restore();
console.log(myTest.test(i++));
The output is:
stub(obj)
1
undefined
3
undefined
5
This one doesn't:
const sinon = require('sinon');
const sandbox = sinon.createSandbox();
console.log('stub(class)')
let i = 1;
class MyTest {
static test(value) {
return value;
}
}
console.log(MyTest.test(i++));
sandbox.stub(MyTest);
console.log(MyTest.test(i++));
sandbox.restore();
console.log(MyTest.test(i++));
sandbox.stub(MyTest);
console.log(MyTest.test(i++));
sandbox.restore();
console.log(MyTest.test(i++));
The output is:
stub(class)
1
undefined
undefined
./node_modules/sinon/lib/sinon/util/core/wrap-method.js:82
throw error;
^
TypeError: Attempted to wrap test which is already wrapped
...
Expected behavior
It would be great if sandbox.restore() would also restore all methods of a class and not only of a object.
Context (please complete the following information):
- Library version: 11.1.1
- Environment: Linux - node v14.16.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source file or test is named in the issue. Start by running the supplied object and class examples against sinon 11.1.1, then trace the sandbox.stub() and sandbox.restore() entry points; done means static methods can be restored and the class example can be stubbed again without an “already wrapped” error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100