From 5ca6d3287d1b11179ee8bc95853c394b2d87b1ce Mon Sep 17 00:00:00 2001 From: Kazuhito Hokamura Date: Sun, 24 Mar 2024 19:09:29 +0900 Subject: [PATCH] Fix README --- README.md | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 5dcef6e..b90dccc 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,6 @@ $ npm install --save-dev @ubie/prisma-cleaner ### with jest -Example code is [here](https://github.com/ubie-oss/prisma-cleaner/blob/main//example/with-jest). - #### Application code ```typescript @@ -52,11 +50,16 @@ export const prisma = new PrismaClient(); ``` ```typescript -// ./src/user.ts -import { prisma } from "./client"; - -export function createUser(data: { name: string }) { - return prisma.user.create({ data }); +// ./src/UserService.ts +import type { PrismaClient } from "@prisma/client"; + +export class UserService { + constructor(private readonly prisma: PrismaClient) {} + createUser(name: string) { + return this.prisma.user.create({ + data: { name }, + }); + } } ``` @@ -66,13 +69,13 @@ export function createUser(data: { name: string }) { // jest.config.ts const config: Config = { // ... - globalSetup: "/global-setup.ts", - setupFilesAfterEnv: ["/setup.ts"], + globalSetup: "/test/global-setup.ts", + setupFilesAfterEnv: ["/test/setup.ts"], }; ``` ```typescript -// cleander.ts +// ./test/cleander.ts import { Prisma, PrismaClient } from "@prisma/client"; import { PrismaCleaner } from "@ubie/prisma-cleaner"; @@ -83,7 +86,7 @@ export const cleaner = new PrismaCleaner({ ``` ```typescript -// global-setup.ts +// ./test/global-setup.ts import { cleaner } from "./cleaner"; export default async function setup() { @@ -93,16 +96,10 @@ export default async function setup() { ``` ```typescript -// setup.ts +// ./test/setup.ts import { PrismaClient } from "@prisma/client"; import { cleaner } from "./cleaner"; -jest.mock("./src/client", () => { - return { - prisma: new PrismaClient().$extends(cleaner.withCleaner()), - }; -}); - afterEach(async () => { await cleaner.cleanup(); }); @@ -111,15 +108,19 @@ afterEach(async () => { #### Test code ```typescript -// ./src/user.test.ts -import { prisma } from "./client"; -import { createUser } from "./user"; +// ./src/UserService.test.ts +import { PrismaClient } from "@prisma/client"; +import { UserService } from "./UserService"; +import { cleaner } from "../test/cleaner"; + +describe("UserService", () => { + const prisma = new PrismaClient().$extends(cleaner.withCleaner()) as PrismaClient; + const userService = new UserService(prisma); -describe("user", () => { it("should create a new user", async () => { // this record will delete by prisma-cleaner in afterEach defined by setup.ts - const created = await createUser("xxx"); - expect(created.name).toEqual("xxx"); + const user = await userService.createUser("xxx"); + expect(user.name).toEqual("xxx"); expect(await prisma.user.count()).toEqual(1); }); @@ -130,6 +131,8 @@ describe("user", () => { }); ``` +See more [examples](https://github.com/ubie-oss/prisma-cleaner/blob/main//example/with-jest). + ### manually cleanup Prisma-cleaner adds tables targeted for deletion triggered by the execution of Prisma's `create`, `createMany`, `upsert`. If added via `$executeRaw` or similar, they will not be automatically deleted, so you will need to manually delete them.