### Install Mockingoose Source: https://github.com/alonronin/mockingoose/blob/master/README.md Install the package as a development dependency. ```bash npm i mockingoose -D ``` -------------------------------- ### Reset All Mocks Source: https://github.com/alonronin/mockingoose/blob/master/README.md Clear all mocks globally, typically used in test setup hooks. ```ts beforeEach(() => { mockingoose.resetAll(); }); ``` -------------------------------- ### Import Mockingoose Source: https://github.com/alonronin/mockingoose/blob/master/README.md Import the library using either ESM/TypeScript or CommonJS syntax. ```ts // ESM / TypeScript import mockingoose from 'mockingoose'; // CommonJS const mockingoose = require('mockingoose'); ``` -------------------------------- ### Mock Cursor Support Source: https://github.com/alonronin/mockingoose/blob/master/README.md Mock cursor operations including next(), for await...of, and eachAsync. ```ts 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); }); ``` -------------------------------- ### Mock Model Operations with Functions Source: https://github.com/alonronin/mockingoose/blob/master/README.md Pass a function to toReturn to dynamically inspect the query and return results based on input. ```ts import mockingoose from 'mockingoose'; import User from './user'; describe('test mongoose User model', () => { it('should return the doc with findById', async () => { const _doc = { _id: '507f191e810c19729de860ea', name: 'name', email: 'name@email.com', }; const finderMock = (query: any) => { if (query.getQuery()._id === '507f191e810c19729de860ea') { return _doc; } }; mockingoose(User).toReturn(finderMock, 'findOne'); const doc = await User.findById('507f191e810c19729de860ea'); expect(JSON.parse(JSON.stringify(doc))).toMatchObject(_doc); }); }); ``` -------------------------------- ### Mock populate operations Source: https://github.com/alonronin/mockingoose/blob/master/README.md Change the schema path to Object or Mixed to allow mocking populated fields in query results. ```ts User.schema.path('foreignKey', Object); const doc = { email: 'test@mail.com', foreignKey: { _id: '5ca4af76384306089c1c30ba', name: 'test', value: 'test', }, name: 'Name', saveCount: 1, }; mockingoose(User).toReturn(doc); const result = await User.find(); expect(result).toMatchObject(doc); ``` -------------------------------- ### mockingoose.resetAll() Source: https://github.com/alonronin/mockingoose/blob/master/README.md Resets all mock configurations across all models. ```APIDOC ## mockingoose.resetAll() ### Description Clears all mock configurations globally. This is typically used in a `beforeEach` or `afterEach` block to ensure test isolation. ``` -------------------------------- ### Simulate errors with mockingoose Source: https://github.com/alonronin/mockingoose/blob/master/README.md Use toReturn with an Error object to test rejection handling in Mongoose operations. ```ts mockingoose(User).toReturn(new Error('My Error'), 'save'); await expect( User.create({ name: 'name', email: 'name@email.com' }) ).rejects.toThrow('My Error'); ``` -------------------------------- ### mockingoose(Model).toReturn(obj | fn, operation) Source: https://github.com/alonronin/mockingoose/blob/master/README.md Configures a mock return value for a specific Mongoose model operation. The return value can be a static object or a function that receives the query object. ```APIDOC ## mockingoose(Model).toReturn(obj | fn, operation) ### Description Sets the return value for a specific Mongoose operation on a given model. If a function is provided, it receives the Mongoose Query or Aggregate object to allow dynamic responses. ### Parameters - **Model** (Object) - Required - The Mongoose model to mock. - **obj | fn** (Object | Function) - Required - The data to return or a function that returns data based on the query. - **operation** (String) - Optional - The Mongoose operation to mock (defaults to 'find'). Supported operations include 'find', 'findOne', 'save', 'updateOne', 'aggregate', etc. ``` -------------------------------- ### Mock Model Operations with Objects Source: https://github.com/alonronin/mockingoose/blob/master/README.md Use toReturn to provide a static object response for specific Mongoose operations. ```ts // __tests__/user.test.ts import mockingoose from 'mockingoose'; import User from './user'; describe('test mongoose User model', () => { it('should return the doc with findById', async () => { const _doc = { _id: '507f191e810c19729de860ea', name: 'name', email: 'name@email.com', }; mockingoose(User).toReturn(_doc, 'findOne'); const doc = await User.findById('507f191e810c19729de860ea'); expect(JSON.parse(JSON.stringify(doc))).toMatchObject(_doc); }); it('should return the doc with updateOne', async () => { const _doc = { ok: 1, nModified: 1, n: 1 }; mockingoose(User).toReturn(_doc, 'updateOne'); const result = await User.updateOne({ name: 'changed' }, {}); expect(result).toMatchObject(_doc); }); }); ``` -------------------------------- ### Chain Mock Operations Source: https://github.com/alonronin/mockingoose/blob/master/README.md Chain multiple toReturn calls and resets on a single model instance. ```ts mockingoose(User) .toReturn({ name: 'name' }) .toReturn({ name: 'a name too' }, 'findOne') .toReturn({ name: 'another name' }, 'save') .reset('find'); ``` -------------------------------- ### mockingoose(Model).reset(operation) Source: https://github.com/alonronin/mockingoose/blob/master/README.md Resets the mock configuration for a specific model, optionally restricted to a single operation. ```APIDOC ## mockingoose(Model).reset(operation) ### Description Clears the mock state for the specified model. If an operation name is provided, only that operation's mock is cleared. ### Parameters - **operation** (String) - Optional - The specific operation to reset. If omitted, all operations for the model are reset. ``` -------------------------------- ### Reset Mocks Source: https://github.com/alonronin/mockingoose/blob/master/README.md Reset mocks for specific operations or the entire model. ```ts 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 ``` -------------------------------- ### Define Mongoose Model Source: https://github.com/alonronin/mockingoose/blob/master/README.md Standard Mongoose model definition for use with mocking. ```ts // 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); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.