P
Prisma3mo ago
Osamu

Mocking prisma client with javascript

How can I mock prisma client with javascript? The guides I found are heavily depended on typescript
Solution:
Hi @Osamu Can you take a look at this repo ? I created a mock example in Javascript using Prisma....
GitHub
GitHub - ludralph/mocking_playground
Contribute to ludralph/mocking_playground development by creating an account on GitHub.
Jump to solution
12 Replies
Solution
RaphaelEtim
RaphaelEtim3mo ago
Hi @Osamu Can you take a look at this repo ? I created a mock example in Javascript using Prisma.
GitHub
GitHub - ludralph/mocking_playground
Contribute to ludralph/mocking_playground development by creating an account on GitHub.
RaphaelEtim
RaphaelEtim3mo ago
It's based off of the guides but i used Javascript instead of Typescript.
Osamu
OsamuOP3mo ago
That's actually pretty cool thanks man I hope this gets into the documentation soon
RaphaelEtim
RaphaelEtim3mo ago
I'll add a similar example to the Prisma Example repository so it can be easy for our users to find.
GitHub
GitHub - prisma/prisma-examples: 🚀 Ready-to-run Prisma example proj...
🚀 Ready-to-run Prisma example projects. Contribute to prisma/prisma-examples development by creating an account on GitHub.
Osamu
OsamuOP3mo ago
Pardon but it should work with es6 right? Or does it need more configuration?
Osamu
OsamuOP3mo ago
I've tried to implement it I kept getting problem related to ES6 I couldn't mock the prisma client sadly
RaphaelEtim
RaphaelEtim3mo ago
Can you please share the error you're facing?
Osamu
OsamuOP3mo ago
/home/mohammad/Desktop/ALX/HealthVault/tests/patient.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import PatientController from "../controllers/PatientController"
^^^^^^

SyntaxError: Cannot use import statement outside a module

at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
/home/mohammad/Desktop/ALX/HealthVault/tests/patient.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import PatientController from "../controllers/PatientController"
^^^^^^

SyntaxError: Cannot use import statement outside a module

at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
I'm trying to use es6
RaphaelEtim
RaphaelEtim3mo ago
You need to configure Jest to support ES6 module. You could check resources online on how to do that
Osamu
OsamuOP3mo ago
Thank you
RaphaelEtim
RaphaelEtim3mo ago
You're welcome
Osamu
OsamuOP3mo ago
Testing with ES6 requires to install jest-mock-extended and to create a file, the name can be anything:
import { mockDeep, mockReset } from "jest-mock-extended";

import { prisma } from './utils/prisma'

jest.mock('./utils/prisma', () => ({
prisma: mockDeep(),
// The line to activate es6, without it you'll get an error
__es6: true
}))

beforeEach(() => {
mockReset(prismaMock)
})

const prismaMock = prisma

export default prismaMock
import { mockDeep, mockReset } from "jest-mock-extended";

import { prisma } from './utils/prisma'

jest.mock('./utils/prisma', () => ({
prisma: mockDeep(),
// The line to activate es6, without it you'll get an error
__es6: true
}))

beforeEach(() => {
mockReset(prismaMock)
})

const prismaMock = prisma

export default prismaMock
Then, you can mock the api as you like

Did you find this page helpful?