This is a mock for PrismaClient
. It actually reads your schema.prisma
and generate models based on it.
It perfectly simulates Prisma's API and store everything in-memory for fast, isolated, and retry-able unit tests.
It's heavily tested, by comparing the mocked query results with real results from prisma. Tested environments include MySQL
, PostgreSQL
and MongoDB
.
This library can also be used as an in-memory implementation of Prisma, for reasons such as prototyping, but that's not its primary goal.
After setting up Prisma:
yarn
$ yarn add -D prismock
npm
$ npm add --save-dev prismock
There is two options here, depending on your application architecture.
You can mock the PrismaClient directly (Example):
import { PrismaClient } from '@prisma/client';
import { generatePrismock } from 'prismock';
jest.mock('@prisma/client', () => {
return {
...jest.requireActual('@prisma/client'),
PrismaClient: jest.fn(),
};
});
beforeAll(async () => {
const prismock = await generatePrismock();
(PrismaClient as jest.Mock).mockReturnValue(prismock);
});
If you are using dependency injection, you can directly use prismock
. I personally do so with the amazing NestJS:
import { generatePrismock } from 'prismock';
let app: INestApplication;
beforeAll(async () => {
const prismock = await generatePrismock();
const moduleRef = await Test.createTestingModule({ imports: [] })
.overrideProvider(PrismaService)
.useValue(prismock)
.compile();
app = moduleRef.createNestApplication();
await app.init();
});
Then, you will be able to write your tests as if your app was using an in-memory Prisma client.
You may have the option to generate the Prismock client synchronously if you have access to the DMMF (Datamodel Meta Format).
import { generatePrismockSync } from 'prismock';
import { Prisma } from '__generated__/client';
const models = Prisma.dmmf.datamodel.models;
const prismock = generatePrismockSync({ models });
generatePrismock(
pathToSchema?: string,
): Promise<PrismaClient>
The returned object is similar to a PrismaClient, which can be used as-is.
Path to the schema file. If not provided, the schema is prisma/schema.prisma
.
generatePrismockSync(
models?: DMMF.Model[],
): PrismaClient
The returned object is similar to a PrismaClient, which can be used as-is.
List of models extracted from the DMMF document.
Two additional functions are returned with the PrismaClient, getData
and setData
. In some edge-case, we need to directly access, or change, the data store management by prismock.
Most of the time, you won't need it in your test, but keep in mind they exist
const prismock = await generatePrismock();
prismock.setData({ user: [] });
const prismock = await generatePrismock();
prismock.getData(); // { user: [] }
Feature | State |
---|---|
findUnique | ✔ |
findFirst | ✔ |
findMany | ✔ |
create | ✔ |
createMany | ✔ |
delete | ✔ |
deleteMany | ✔ |
update | ✔ |
updateMany | ✔ |
upsert | ✔ |
count | ✔ |
aggregate | ✔ |
groupBy | ⛔ |
Feature | State |
---|---|
distinct | ✔ |
include | ✔ |
where | ✔ |
select | ✔ |
orderBy | ⛔ |
select + count | ⛔ |
Feature | State |
---|---|
create | ✔ |
createMany | ✔ |
update | ✔ |
updateMany | ✔ |
connect | ✔ |
connectOrCreate | ✔ |
set | ⛔ |
disconnect | ⛔ |
upsert | ⛔ |
delete | ⛔ |
Feature | State |
---|---|
equals | ✔ |
gt | ✔ |
gte | ✔ |
lt | ✔ |
lte | ✔ |
not | ✔ |
in | ✔ |
notIn | ✔ |
contains | ✔ |
startWith | ✔ |
endsWith | ✔ |
AND | ✔ |
OR | ✔ |
NOT | ✔ |
mode | ✔ |
search | ⛔ |
Feature | State |
---|---|
some | ✔ |
every | ✔ |
none | ✔ |
is | ⛔ |
Feature | State |
---|---|
set | ⛔ |
push | ⛔ |
Feature | State |
---|---|
has | ⛔ |
hasEvery | ⛔ |
hasSome | ⛔ |
isEmpty | ⛔ |
equals | ⛔ |
Feature | State |
---|---|
increment | ✔ |
decrement | ✔ |
multiply | ✔ |
divide | ✔ |
set | ✔ |
Feature | State |
---|---|
path | ⛔ |
string_contains | ⛔ |
string_starts_withn | ⛔ |
string_ends_with | ⛔ |
array_contains | ⛔ |
array_starts_with | ⛔ |
array_ends_with | ⛔ |
Feature | State |
---|---|
@@id | ✔ |
@default | ✔ |
@relation | ✔ |
@unique | ⛔ |
@@unique | ⛔ |
@updatedAt | ⛔ |
Feature | State |
---|---|
autoincrement() | ✔ |
now() | ✔ |
uuid() | ✔ |
auto() | ⛔ |
cuid() | ⛔ |
dbgenerated | ⛔ |
Feature | State |
---|---|
onDelete (SetNull, Cascade) | ✔ |
onDelete (Restrict, NoAction, SetDefault)() | ⛔ |
onUpdate | ⛔ |
- Complete supported features.
- Refactoring of update operation.
- Replace item formating with function composition
- Restore test on
_count
for mongodb - Add custom client method for MongoDB (
$runCommandRaw
,findRaw
,aggregateRaw
)
While Prisma is amazing, its unit testing
section is treated as optional. On the other hand, it should be a priority for developers to write tests.
As I love Prisma, I decided to create this package, in order to keep using it on real-world projects.
I'm also a teacher and believe it's mandatory for students to learn about testing. I needed a similar solution for my backend course, so I created my own.
I'm personally using this library in my day-to-day activities, and add features or fix bugs depending on my needs.
If you need unsupported features or discover unwanted behaviors, feel free to open an issue, I'll take care of it.
Inspired by prisma-mock.