1.31.0 • Published 4 days ago

prismock v1.31.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 days ago

prismock

npm Build npm

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.

Installation

After setting up Prisma:

yarn

$ yarn add -D prismock

npm

$ npm add --save-dev prismock

Usage

There are a few options here, depending on your application architecture.

Automatically (recommended)

You can create a __mocks__ directory at the root of your project, with a sub-directory named @prisma. Inside the @prisma directory, create a client.js file (or client.ts for TypeScript).

Inside the client file, you can re-export the @prisma/client module, and replace PrismaClient by PrismockClient:

import { PrismockClient } from 'prismock';

export * from '@prisma/client';
export { PrismockClient as PrismaClient };

That's it, prisma will be mocked in all your tests (tested with Jest & ViTest)

PrismaClient

You can mock the PrismaClient directly in your test, or setupTests (Example):

jest.mock('@prisma/client', () => {
  return {
    ...jest.requireActual('@prisma/client'),
    PrismaClient: jest.requireActual('prismock').PrismockClient,
  };
});

Use prismock manually

You can instantiate a PrismockClient directly and use it in your test, or pass it to a test version of your app.

import { PrismockClient } from 'prismock';

import { PrismaService } from './prisma.service';

const prismock = new PrismockClient();
const app = createApp(prismock);

Then, you will be able to write your tests as if your app was using an in-memory Prisma client.

Using custom client path

If you are using a custom client path, you need the createPrismock method.

Internal data

Two additional functions are returned compared to the PrismaClient, getData, and reset. In some edge-case, we need to directly access, or reset, 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 = new PrismockClient();
prismock.getData(); // { user: [] }
const prismock = new PrismockClient();
prismock.reset(); // State of prismock back to its original

Supported features

Model queries

FeatureState
findUnique
findFirst
findMany
create
createMany
delete
deleteMany
update
updateMany
upsert
count
aggregate
groupBy💬 note

Model query options

FeatureState
distinct
include
where
select
orderBy (Partial)
select + count

Nested queries

FeatureState
create
createMany
update
updateMany
connect
connectOrCreate
upsert
set
disconnect
delete

Filter conditions and operators

FeatureState
equals
gt
gte
lt
lte
not
in
notIn
contains
startWith
endsWith
AND
OR
NOT
mode
search

Relation filters

FeatureState
some
every
none
is

Scalar list methods

FeatureState
set
push

Scalar list filters

FeatureState
has
hasEvery
hasSome
isEmpty
equals

Atomic number operations

FeatureState
increment
decrement
multiply
divide
set

JSON filters

FeatureState
path
string_contains
string_starts_withn
string_ends_with
array_contains
array_starts_with
array_ends_with

Attributes

FeatureState
@@id
@default
@relation
@unique
@@unique
@updatedAt

Attribute functions

FeatureState
autoincrement()
now()
uuid()
auto()
cuid()
dbgenerated

Referential actions

FeatureState
onDelete (SetNull, Cascade)
onDelete (Restrict, NoAction, SetDefault)()
onUpdate

Notes

groupBy Support

Basic groupBy queries are supported, including having and orderBy. skip, take, and cursor are not yet supported.

Roadmap

  • Complete supported features.
  • Refactoring of update operation.
  • Replace item formatting with function composition
  • Restore test on _count for mongodb
  • Add custom client method for MongoDB ($runCommandRaw, findRaw, aggregateRaw)

Motivation

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.

Feature request

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.

Credit

Inspired by prisma-mock.

1.31.0

4 days ago

1.30.4

6 days ago

1.30.3

12 days ago

1.30.2

1 month ago

1.30.1

2 months ago

1.30.0

2 months ago

1.29.0

2 months ago

1.29.1

2 months ago

1.28.0

3 months ago

1.27.0

3 months ago

1.26.4

3 months ago

1.26.3

3 months ago

1.26.2

3 months ago

1.26.1

4 months ago

1.26.0

4 months ago

1.25.0

4 months ago

1.24.0

5 months ago

1.22.0

7 months ago

1.21.1

8 months ago

1.23.2

6 months ago

1.23.0

6 months ago

1.21.2

7 months ago

1.23.1

6 months ago

1.21.3

7 months ago

1.15.0

9 months ago

1.14.1

10 months ago

1.14.0

11 months ago

1.19.0

9 months ago

1.18.0

9 months ago

1.17.0

9 months ago

1.16.0

9 months ago

1.19.2

8 months ago

1.19.1

8 months ago

1.21.0

8 months ago

1.20.0

8 months ago

1.13.2

12 months ago

1.13.1

1 year ago

1.13.0

1 year ago

1.12.0

1 year ago

1.11.0

1 year ago

1.10.0

1 year ago

1.9.1

1 year ago

1.9.0

1 year ago

1.8.1

1 year ago

1.8.0

1 year ago

1.7.0

1 year ago

1.6.1

1 year ago

1.5.2

1 year ago

1.6.0

1 year ago

1.9.3

1 year ago

1.9.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.3.2

1 year ago

1.4.0

1 year ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago