一个用于模拟 Mongoose 模型的包 — 可与 Jest 和 Vitest 兼容
A package for mocking Mongoose models — works with Jest and Vitest
npm i mockingoose -D
// ESM / TypeScript
import mockingoose from 'mockingoose';
// CommonJS
const mockingoose = require('mockingoose');
// user.ts
import mongoose, { Schema } from 'mongoose';
const schema = new Schema({
name: String,
email: String,
created: { type: Date, default: Date.now },
});
export default mongoose.model('User', schema);
Returns a plain object.
…
Allows passing a function in order to return the result.
You will be able to inspect the query using the parameter passed to the function. This will be either a Mongoose Query or Aggregate class, depending on your usage.
…
Will reset Model mock. If passed an operation, will reset only that operation's mock.
mockingoose(User).toReturn({ name: '1' });
mockingoose(User).toReturn({ name: '2' }, 'save');
mockingoose(User).reset(); // will reset all operations
mockingoose(User).reset('find'); // will reset only find operation
You can also chain operations:
mockingoose(User)
.toReturn({ name: 'name' })
.toReturn({ name: 'a name too' }, 'findOne')
.toReturn({ name: 'another name' }, 'save')
.reset('find');
Will reset all mocks.
beforeEach(() => {
mockingoose.resetAll();
});
find - for find queryfindOne - for findOne querycountDocuments - for count queryestimatedDocumentCount - for count collection documentsdistinct - for distinct queryfindOneAndUpdate - for findOneAndUpdate queryfindOneAndDelete - for findOneAndDelete queryfindOneAndReplace - for findOneAndReplace queryupdateOne - for updateOne queryupdateMany - for updateMany querysave - for create, and save documents Model.create() or Model.save() or doc.save()deleteOne - for deleteOne querydeleteMany - for deleteMany queryaggregate - for aggregate frameworkreplaceOne - for replaceOne queryinsertMany - for Model.insertMany() bulk insert, can also pass { lean: true, rawResult: true } options.bulkWrite - for Model.bulkWrite() bulk operationsbulkSave - for Model.bulkSave() bulk saveYou can mock cursor() on find queries. The cursor uses the find mock data and supports next(), eachAsync(), close(), and for await...of:
mockingoose(User).toReturn([{ name: 'a' }, { name: 'b' }]);
// next()
const cursor = User.find().cursor();
const first = await cursor.next();
const second = await cursor.next();
const done = await cursor.next(); // null
// for await...of
for await (const doc of User.find().cursor()) {
console.log(doc);
}
// eachAsync
await User.find()
.cursor()
.eachAsync((doc) => {
console.log(doc);
});
All operations work with exec and promise patterns.
If you are using Model.create and you don't pass a mock with mockingoose you'll receive the mongoose created doc (with ObjectId and transformations)
Validations work as expected.
The returned document is an instance of mongoose Model.
deleteOne and updateOne operations return the original mocked object.
You can simulate errors by passing an Error to mockingoose:
mockingoose(User).toReturn(new Error('My Error'), 'save');
await expect(
User.create({ name: 'name', email: '[email protected]' })
).rejects.toThrow('My Error');
You can mock .populate in your mocked result — just change the Schema's path to appropriate type (e.g., Object | Mixed):
User.schema.path('foreignKey', Object);
const doc = {
email: '[email protected]',
foreignKey: {
_id: '5ca4af76384306089c1c30ba',
name: 'test',
value: 'test',
},
name: 'Name',
saveCount: 1,
};
mockingoose(User).toReturn(doc);
const result = await User.find();
expect(result).toMatchObject(doc);
You can mock Model.exists() via the findOne operation. See Issue #69
No connection is made to the database (mongoose.connect is mocked)
Requires Node.js 18+ and Mongoose 9+. Tested with Vitest and Jest.
count, update, remove, findOneAndRemoveimport or require)mockingoose.ModelName is deprecated, mockingoose(Model) is the recommended usage, with Model being a Mongoose model class.
Alternatively, you may pass a string with the model name.
mockingoose(Model).toReturn((query) => value) can also take a function as a parameter.
The function is called with either a Query or Aggregate object from Mongoose, depending on the request. This allows tests to ensure that proper queries are sent out, and helps with regression testing.
暂无开放 Issues,或尚未同步最近议题。