Closed regevbr closed 3 years ago
much needed!
You can do this with mockDeep()
now.
Hey @marchaos, could you please provide an example of this? I can't see any on the docs.
For my use case I'm using mongodb
. This is the function I'd like to test:
// getUser.ts
import { Db } from 'mongodb';
export async function getUser(db: Db, userId: string) {
const user = await db.collection('users').findOne({ id: userId });
if (!user) {
throw 'User not found';
}
return user;
}
I'd like to be able to do something like this:
// getUser.test.ts
import { mockDeep } from 'jest-mock-extended';
import { Db } from 'mongodb';
import { getUser } from './getUser';
it('should throw error if user not found', async () => {
const mockDb = mockDeep<Db>();
// Alternative 1
const mockUserCollection = mockDb.collection.calledWith('users');
const mockFindOne = mockUserCollection.findOne.calledWith({ id: 'no-such-id' }).mockResolvedValue(null);
// Alternative 2
const mockFindOne = mockDb.collection.findOne.mockResolvedValue(null);
expect(await getUser(mockDb, 'no-such-id')).toThrow('User not found');
expect(mockFindOne).toHaveBeenCalledTimes(1);
});
Both alternatives are returning compile errors for me 😞 .
Thanks for the amazing lib, it's been great working with it 😄
Currently, when creating a deep mock, there is no way to control the mocks of return values of first level mocked functions.
Essentially I want to be able to do: