Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide documentation/support for dynamic mocking of return values in tests #6603

Open
cebjyre opened this issue Apr 26, 2022 · 4 comments
Open

Comments

@cebjyre
Copy link

cebjyre commented Apr 26, 2022

Problem

I'd like to have the unit testing of code working with prisma be more precise about what is being passed into and returned from the mocked prisma method. The examples in the documentation make use of mockResolvedValue (with the same data passed into the tested object as is returned by the prisma method) but I'd like to use mockImplementation so I can ensure the prisma method is working with the data that was actually supplied to it.

Suggested solution

Allow (or document if it is already possible) alternative mocking techniques.

Additional context

The suggested mocking solution

test('should create new user ', async () => {
  const user = {
    id: 1,
    name: 'Rich',
    email: '[email protected]',
    acceptTermsAndConditions: true,
  }

  prismaMock.user.create.mockResolvedValue(user)

  await expect(createUser(user)).resolves.toEqual({
    id: 1,
    name: 'Rich',
    email: '[email protected]',
  })
})

would still pass even if the implementation of createUser was:

export async function createUser(user: CreateUser) {
  return await prisma.user.create({
    data: {id: user.id, name: user.name},
  });
}

i.e. the failure by the function to pass the email value down to prisma is not shown by the test.

I've tried using create.mockImplementation but get the equivalent of

Type 'Promise<User>' is missing the following properties from type 'Prisma__UserClient<User>': _dmmf, _fetcher, _queryType, _rootField, and 10 more.
@huv1k
Copy link
Contributor

huv1k commented Jun 2, 2023

I would love to see it too, because right now unit testing is really difficult to achieve with more complex problems. Right now there are really simple use-cases possible with unit tests.

@patrykk21
Copy link

Same here

@henrikra
Copy link

henrikra commented Jul 2, 2024

Using mockImplementation works but you have to type the return value of mockImplementation with any 😕

@jkomyno
Copy link
Contributor

jkomyno commented Jan 17, 2025

We should also document how to mock values within a query extension: prisma/prisma#23394.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants