My approach involves using some abstractions where the code receives a User, converts it to Mongo format (adding an underscore to the id generated elsewhere), saves it, and then returns the saved User without the underscore in the id:
constructor(
@InjectModel('User')
private readonly service: typeof Model
) { }
async saveUser(user: User): Promise<User> {
const mongoUser = this.getMongoUser(user);
const savedMongoUser = await new this.service(mongoUser).save();
return this.toUserFormat(savedMongoUser);
}
The test scenario I'm working on:
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
MongoUserRepository,
{
provide: getModelToken('User'),
useValue: { ... }, // all functions used with jest.fn()
},
],
}).compile();
service = module.get<MongoUserRepository>(MongoUserRepository);
model = module.get<Model<UserDocument>>(getModelToken('User'));
});
it('should save a new user', async () => {
jest.spyOn(model, 'save').mockReturnValue({
save: jest.fn().mockResolvedValueOnce(mockMongoFormat)
} as any);
const foundMock = await service.saveUser(mockUserFormat);
expect(foundMock).toEqual(mockUserFormat);
});
The problems encountered:
An error stating that no overload matches this call.
Option 1 of 4, '(object: Model<UserDocument, {}>, method: "model" | "remove" | "deleteOne" | "init" | "populate" | "replaceOne" | "update" | "updateOne" | "addListener" | "on" | ... 45 more ... | "where"): SpyInstance<...>', triggered this issue.
The argument '"save"' is not suitable for the parameter '"model" | "remove" | "deleteOne" | "init" | "populate" | "replaceOne" | "update" | "updateOne" | "addListener" | "on" | "once" | "removeListener" | "off" | "removeAllListeners" | ... 41 more ... | "where"'.
Option 2 of 4, '(object: Model<UserDocument, {}>, method: "collection"): SpyInstance<Collection, [name: string, conn: Connection, opts?: any]>', resulted in this error.
The argument '"save"' is not suitable for the parameter '"collection"'.ts(2769)
Attempts to use "new" also faced challenges:
An error indicating that no overload matches this call.
Option 1 of 4, '(object: Model<UserDocument, {}>, method: "find" | "watch" | "translateAliases" | "bulkWrite" | "model" | "$where" | "aggregate" | "count" | "countDocuments" | ... 46 more ... | "eventNames"): SpyInstance<...>', triggered this problem.
The argument '"new"' is not suitable for the parameter '"find" | "watch" | "translateAliases" | "bulkWrite" | "model" | "$where" | "aggregate" | "count" | "countDocuments" | "estimatedDocumentCount" | "create" | "createCollection" | ... 43 more ... | "eventNames"'.
Option 2 of 4, '(object: Model<UserDocument, {}>, method: "collection"): SpyInstance<Collection, [name: string, conn: Connection, opts?: any]>', resulted in this error.
The argument '"new"' is not suitable for the parameter '"collection"'.
I might need to reconsider my implementation, but I am keen on discovering how to proceed in this situation. How can I effectively mock that function?