Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
prisma-mock-vitest
Advanced tools
This is a mock of the Prisma API, intended for unit testing with Vitest. All the data is stored in memory.
This library uses vitest-mock-extended, so if functionality you need is not implemented yet, you can mock it yourself.
Pull requests are welcome for missing features.
yarn add -D prisma-mock-vitest
npm install prisma-mock-vitest --save-dev
You should also have run npx prisma generate
for your Prisma schema.
Simple example how to create a prisma mock instance:
import { createPrismaMock } from 'prisma-mock-vitest';
import { beforeEach, expect, test } from 'vitest';
let client: PrismaClient;
beforeEach(async () => {
client = await createPrismaMock();
});
test('Initial user count is 0', async () => {
const totalUsers = await client.user.count();
expect(totalUsers).toEqual(0);
});
An example how to mock a global prisma instance inside and schema a 'db' directory (like blitzjs):
import type { PrismaClient } from '@prisma/client';
import { createPrismaMock } from 'prisma-mock-vitest';
import { beforeEach } from 'vitest';
import { mockDeep, mockReset } from 'vitest-mock-extended';
vi.mock('db', () => ({
__esModule: true,
...jest.requireActual('db'),
default: mockDeep<PrismaClient>(),
}));
import db, { Prisma } from 'db';
beforeEach(() => {
mockReset(db);
return createPrismaMock({}, Prisma.dmmf.datamodel, db);
});
createPrismaMock<P extends PrismaClient = PrismaClient>(
data: PrismaMockData<P> = {},
datamodel?: Prisma.DMMF.Datamodel,
client = mockDeep<P>(),
): Promise<P>;
Object with an array default data for each table/model.
createPrismaMock({
users: [
{
id: 1,
name: 'John Doe',
accountId: 1,
},
],
account: [
{
id: 1,
name: 'Company',
},
],
});
The datamodel of the prisma client, i.e. Prisma.dmmf.datamodel
.
vitest-mock-extended
instance. If it's not provided, a new instance is created.
A lot of the functionality is implemented, but parts are missing. Here is a list of the (missing) features.
TODO (set, push)
TODO (has, hasEvery, hasSome, isEmpty, equals)
TODO (path, string_contains, string_starts_with, string_ends_with, array_contains, array_starts_with, array_ends_with)
FAQs
Mock prisma client for unit testing with vitest
We found that prisma-mock-vitest demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.