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.

Is there a way to mock populate for mongoose?

See original GitHub issue

Schema:

const  mongoose = require('mongoose');
const { Schema } = mongoose;

const user = Schema({
    name: String,
    email: String,
    foo: { type: String, ref: 'Foo' },
    created: { type: Date, default: Date.now }
})

const foo = Schema({
    name: String,
    email: String,
    created: { type: Date, default: Date.now }
})

const User = mongoose.model('User', user);
const Foo = mongoose.model('Foo', foo);

module.exports = { User, Foo };

Test:

const mockingoose = require('mockingoose').default;
const { User, Foo } = require('./schema');
describe('test mongoose User model', () => {
  it('should return the doc with findById', async () => {
    const _doc = {
        _id: '507f191e810c19729de860ea',
        name: 'name',
        foo: {
          name: 'bar',
        },
        email: 'name@email.com'
    };
    const _doc2 = {
        _id: '507f191e810c19729de860ea',
        name: 'name2',
        foo: {
          name: 'bar',
        },
        email: 'name@email.com'
    };

    const foo = await Foo.create({name: 'bar'});
    _doc.foo = foo._id;

    mockingoose.User.toReturn(_doc, 'findOne');
    // mockingoose.User.toReturn(_doc2, 'populate');
    const user = await User.findOne({ _id: '507f191e810c19729de860ea'}).populate('foo');
    console.log(user);
    // expect(JSON.parse(JSON.stringify(user))).toMatchObject(_doc);
  });
});

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
alonronincommented, Nov 19, 2018

it is not suppose to populate as it is just a mock, so if you use populate, mock the complete result.

0reactions
refextucommented, Nov 21, 2018

The issue was not to try to populate. @alonronin: the fact is: that there is no way to mock the complete result. When I try to mock the response (see above) the result is:

    { _id: 507f191e810c19729de860ea,
      name: 'name',
      foo: '5bf52e68c3d3c8098cd840b1',
      email: 'name@email.com',
      created: 2018-11-21T10:07:36.290Z }

Even when i try to return _doc2 I’ll have the problem, that I don’t get foo:

    { _id: 507f191e810c19729de860ea,
      name: 'name2',
      email: 'name@email.com',
      created: 2018-11-21T10:10:01.734Z }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit test: How to mock mongoose code when using populate?
Specifically, I am trying to test that the NotFoundError exception is thrown when no matching record is found, but I am confused how...
Read more >
How to Stub mongoose methods and Mock Document Objects
How to Mock mongodb database access functions. From the cost perspective, the least database read/write operations the better.
Read more >
How to test mongoose models with jest and mockingoose
Now you want to create your test file, for example books.test.js . Then you will need to import the mockingoose, the model and...
Read more >
Testing Mongoose with Jest
Absolutely do not use timer mocks when testing Mongoose apps. This is especially important if you're using Jest >=25 , which stubs out...
Read more >
NodeJs MongoDb fixture from Mongoose schema - ITNEXT
The next step is to use the data provided by files in the mock data directory to populate the database. ... In the...
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