Skip to content

Commit

Permalink
Fix README
Browse files Browse the repository at this point in the history
  • Loading branch information
hokaccha committed Mar 24, 2024
1 parent 62d9532 commit 5ca6d32
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 },
});
}
}
```

Expand All @@ -66,13 +69,13 @@ export function createUser(data: { name: string }) {
// jest.config.ts
const config: Config = {
// ...
globalSetup: "<rootDir>/global-setup.ts",
setupFilesAfterEnv: ["<rootDir>/setup.ts"],
globalSetup: "<rootDir>/test/global-setup.ts",
setupFilesAfterEnv: ["<rootDir>/test/setup.ts"],
};
```

```typescript
// cleander.ts
// ./test/cleander.ts
import { Prisma, PrismaClient } from "@prisma/client";
import { PrismaCleaner } from "@ubie/prisma-cleaner";

Expand All @@ -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() {
Expand All @@ -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();
});
Expand All @@ -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);
});

Expand All @@ -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.
Expand Down

0 comments on commit 5ca6d32

Please sign in to comment.