Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
Why is sinon still throwing "Attempted to wrap connectMongoose which is already wrapped" after .restore?

I have the following test...

it("basic test", () => {
  stub = sinon.stub(app.mongoose, "connectMongoose").returns(Promise.resolve());
  expect(val.listen.callCount).to.equal(0);
  expect(stub.callCount).to.equal(0);
  app.start();
  expect(stub.callCount).to.equal(1);
  expect(val.listen.callCount).to.equal(1);
  stub.restore();
});
it("Error", () => {
  stub = sinon.stub(app.mongoose, "connectMongoose").returns(Promise.reject(new Error("error")));
  expect(val.listen.callCount).to.equal(0);
  expect(stub.callCount).to.equal(0);
  app.start();
  expect(stub.callCount).to.equal(1);
  expect(val.listen.callCount).to.equal(1);
  stub.restore();
});

But when I run I get

TypeError: Attempted to wrap connectMongoose which is already wrapped

I also tried app.mongoose.connectMongoose.restore() but same result. I also tried sandboxes although those seem deprecated.

How do I restore so I can mock it again?

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!