Prisma1: Mocking Prisma client for unit testing (typescript client)

Created on 21 Jan 2019  路  4Comments  路  Source: prisma/prisma1

Is your feature request related to a problem? Please describe.
Is there a simple way to mock Prisma for unit testing? What are the best practices?

Describe the solution you'd like
For instance it would be neat to be able to create an instance of prisma client with dummy data in memory. Something like that:

import { Prisma } from '@generated/prisma-client';
const db = new Prisma({
  mockData: {users: [{name: "myUser", password: "myUserPassword"}] }
  debug: config.isDev!,
});

export default db;

And we could inject that mocked Prisma instance in the context for unit test purspose and use it like a real Prisma.

Describe alternatives you've considered
At the moment we have to mock every prisma functions like this:

const db = {
  users: args => ({name: "myUser", password: "myUserPassword"}),
  createUser: args => ({...args})
  $exists: {
      users: (args) => true
  }
 ...
}
kinquestion areclienjs areclients

Most helpful comment

Hi @durdenx,

I don't think currently there is a solution baked into prisma for this so we can use this issue as a feature request.

That being said I use this approach currently to mock the prisma client and it works fine. Have a look 馃檪

All 4 comments

Hi @durdenx,

I don't think currently there is a solution baked into prisma for this so we can use this issue as a feature request.

That being said I use this approach currently to mock the prisma client and it works fine. Have a look 馃檪

Thanks @pantharshit00! It's a very useful link 馃憤

This would be important for the Go client as well.

+1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ragnorc picture ragnorc  路  3Comments

Fi1osof picture Fi1osof  路  3Comments

schickling picture schickling  路  3Comments

akoenig picture akoenig  路  3Comments

hoodsy picture hoodsy  路  3Comments