Skip to content

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

Open
@cebjyre

Description

@cebjyre

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: 'hello@prisma.io',
    acceptTermsAndConditions: true,
  }

  prismaMock.user.create.mockResolvedValue(user)

  await expect(createUser(user)).resolves.toEqual({
    id: 1,
    name: 'Rich',
    email: 'hello@prisma.io',
  })
})

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions