question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting MongooseError: Operation `todos.insertOne()` buffering timed out after 10000ms error message

See original GitHub issue

This is the test,

describe("POST /todos", () => {
  describe("Todos", () => {
    test("return created todo", async () => {
      const _data = {
        _id,
        created,
        status: "pending",
        subtasks: [],
        title: "Todo X"
      };

      Mockingoose(Todo).toReturn(_data, "save");

      const body = { title: "Todo X" };
      const data = await Todo.create(body);

      expect(data).toMatchObject(_data);
    }, 15000);
  });
});

The code I am trying to test,

const data = await Todo.create({
  title: req.body.title
});

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
OliverFischercommented, Nov 19, 2021

Hi all,

starting from 6.x the authors of mongoose decided to shadow the original save function with Model.prototype.$save = Model.prototype.save;,see https://github.com/Automattic/mongoose/compare/5.x…master#diff-305dbcc98ad8fc959dd0c0be51280893962af34c2d8b18abc871cffc2d80c5f9R521 so that mocking save does not work anymore. Modifying the index.js code to include $save for the ops enumeration

const ops = ['find', 'findOne', 'count', 'countDocuments', 'estimatedDocumentCount', 'distinct', 'findOneAndUpdate', 'findOneAndDelete', 'findOneAndRemove', 'findOneAndReplace', 'remove', 'update', 'updateOne', 'updateMany', 'deleteOne', 'deleteMany', 'save', 'aggregate', '$save'];

and extending

if (!mock && op === 'save') {
      mock = this;
    }

    if (!mock && op === '$save') {
      mock = this;
    }
 // or ;-) at least a little bit better
if (!mock && op === op.includes('save')) {
      mock = this;
    } 
    

and in line 243:

const instance = ['remove', 'save', '$save'];

let all tests pass with 5.x and 6.x. May be @alonronin you may change the code to make it work with both versions. Sadly I am stuck to mocha and sinon and searched for a way to depict the functionality of this great project to our needs/libs, so i am not able to provide a pull request.

Btw: @alonronin: The solution provided in https://github.com/alonronin/mockingoose/issues/77#issuecomment-937651045 is imho not correct, it simply infiltrates the original behaviour with a non-sense mock, so that a spied call with a correctly mocked result of the original function is not guaranteed.

Greetings and hope it helps.

Olli

2reactions
Parnswircommented, Oct 7, 2021

I am getting the same error after upgrading to mongoose@^6.0.0. On 5.x your test should work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongooseError: Operation `users.insertOne()` buffering timed ...
But recently I am making a simple todo app and I face this error when creating a user. ... insertOne()` buffering timed out...
Read more >
MongooseError: Operation `users.insertOne()` buffering timed ...
This error can be caused by typos in user properties. (I was trying to set "Email" instead of what was defined on my...
Read more >
How to Fix "Buffering timed out after 10000ms" Error in ...
The buffering timeout is usually due to either registering models on a newly created connection but using mongoose.connect() : const mongoose = ...
Read more >
Mongoose error : buffering timed out after 10000ms #9732
Hello, i got this error on mongoose with nodejs : version of mongoose : 5.11.8 I'm trying to save data on my mongodb...
Read more >
operation `users.insertone()` buffering timed out after 10000ms
mongooseerror : operation `users.insertone()` buffering timed out after 10000ms. Add Answer | View In TPC Matrix. Technical Problem Cluster First Answered On ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found