javascriptsinonava

Sinon stub error: "Attempted to wrap post which is already wrapped" even though I'm restoring


I have a set of Ava tests using Sinon for stubs. I have several test cases like this one:

test('makes a post request', t => {
  const postStub = sinon.stub(request, 'post').resolves({ foo: 'bar' });

  ... some test stuff ...

  request.post.restore(); // for good measure
  postStub.restore();
});

With just one such test case, everything works fine. But if I add another test case that stubs request.post, I get an error:

Attempted to wrap post which is already wrapped

I don't know why I'm getting this error, though. After all, I am calling restore on the stub. Is there another step I'm missing?


Solution

  • Turns out this is because Ava runs the tests concurrently. Running the tests serially fixes the issue.