Is there a way to mock populate for mongoose?
See original GitHub issueSchema:
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:
- Created 5 years ago
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
it is not suppose to populate as it is just a mock, so if you use populate, mock the complete result.
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:
Even when i try to return
_doc2
I’ll have the problem, that I don’t getfoo
: