I am using mocha, chai and sinon to test my node-express code.
I am experiencing a weird issue, which looks like sinon cannot restore stubs and so in the next test I get the beknown error
Attempted to wrap <method1> which is already wrapped
Here is what I do
mocha-steps
instead of it()
clause for my test cases, so
that they run sequentially (I wanted to ensure it is not an async
race condition)
Here is a test case:
step('should do stuff', test(function () {
const stub1 = sinon.stub(my_model1, 'method1');
chai.request(server)
.get('/endpoint')
.send(body)
.end(function (err, res) {
stub1.restore();
do_various_assertions(err, res);
});
}));
And another one
step('should do other stuff', test(function () {
const stub1 = sinon.stub(my_model1, 'method1');
chai.request(server)
.get('/endpoint')
.send(slightly_different_body)
.end(function (err, res) {
stub1.restore();
do_various_assertions(err, res);
});
}));
where I get the error above
Attempted to wrap <method1> which is already wrapped
If I comment out the stub in the second case works fine. But why? What am I doing wrong?
Next step should know that previous step has completed, you need to call done
function. In your example second step doesn't wait first one and method1
isn't restored.
step('should do stuff', function (done) {
const stub1 = sinon.stub(my_model1, 'method1');
chai.request(server)
.get('/endpoint')
.send(body)
.end(function (err, res) {
stub1.restore();
do_various_assertions(err, res);
done();
});
});