Last active
February 23, 2021 06:14
-
-
Save heygema/54cfed6d750699f7aaf80e1adce60018 to your computer and use it in GitHub Desktop.
Prisma TDD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PrismaClient } from "@prisma/client"; | |
import { ServerInfo } from "apollo-server"; | |
import { execSync } from "child_process"; | |
import getPort, { makeRange } from "get-port"; | |
import { GraphQLClient } from "graphql-request"; | |
import { nanoid } from "nanoid"; | |
import { join } from "path"; | |
import { Client } from "pg"; | |
import { db } from "../api/db"; | |
import { server } from "../api/server"; | |
type TestContext = { | |
client: GraphQLClient; | |
db: PrismaClient; | |
}; | |
function prismaTestContext() { | |
const prismaBinary = join(__dirname, "..", "node_modules", ".bin", "prisma"); | |
let schema = ""; | |
let databaseUrl = ""; | |
let prismaClient: null | PrismaClient = null; | |
return { | |
async before() { | |
schema = `test_${nanoid()}`; | |
databaseUrl = `postgres://postgres:postgres@localhost:5432/testing?schema=${schema}`; | |
process.env.DATABASE_URL = databaseUrl; | |
execSync(`${prismaBinary} db push --preview-feature`, { | |
env: { | |
...process.env, | |
DATABASE_URL: databaseUrl | |
} | |
}); | |
prismaClient = new PrismaClient(); | |
return prismaClient; | |
}, | |
async after() { | |
const client = new Client({ | |
connectionString: databaseUrl | |
}); | |
await client.connect(); | |
await client.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`); | |
await client.end(); | |
// release prisma client connection | |
await prismaClient?.$disconnect(); | |
} | |
}; | |
} | |
function graphqlTestContext() { | |
let serverInstance: ServerInfo | null = null; | |
return { | |
async before() { | |
const port = await getPort({ port: makeRange(4000, 6000) }); | |
serverInstance = await server.listen({ port }); | |
serverInstance.server.on("close", async () => { | |
db.$disconnect(); | |
}); | |
return new GraphQLClient(`http://localhost:${port}`); | |
}, | |
async after() { | |
serverInstance?.server.close(); | |
} | |
}; | |
} | |
export function createTestContext(): TestContext { | |
let ctx = {} as TestContext; | |
const graphqlCtx = graphqlTestContext(); | |
const prismaCtx = prismaTestContext(); | |
beforeEach(async () => { | |
const client = await graphqlCtx.before(); | |
const db = await prismaCtx.before(); | |
Object.assign(ctx, { | |
client, | |
db | |
}); | |
}); | |
afterEach(async () => { | |
await graphqlCtx.after(); | |
await prismaCtx.after(); | |
}); | |
return ctx; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "nexus-tutorial", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"dev": "ts-node-dev --transpile-only --no-notify api/app.ts", | |
"generate": "ts-node --transpile-only api/schema", | |
"watch-prisma": "nodemon --watch 'prisma/schema.prisma' --exec 'npx prisma generate'", | |
"test": "npm run generate && jest", | |
"build": "tsc" | |
}, | |
"jest": { | |
"preset": "ts-jest", | |
"globals": { | |
"ts-jest": { | |
"diagnostics": { | |
"warnOnly": true | |
} | |
} | |
}, | |
"testEnvironment": "node" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"@prisma/client": "^2.17.0", | |
"apollo-server": "^2.20.0", | |
"graphql": "^15.5.0", | |
"nexus": "^1.0.0", | |
"typescript": "^4.1.5" | |
}, | |
"devDependencies": { | |
"@types/jest": "^26.0.20", | |
"@types/pg": "^7.14.10", | |
"get-port": "^5.1.1", | |
"graphql-request": "^3.4.0", | |
"jest": "^26.6.3", | |
"nanoid": "^3.1.20", | |
"nodemon": "^2.0.7", | |
"pg": "^8.5.1", | |
"prettier": "^2.2.1", | |
"prisma": "^2.17.0", | |
"ts-jest": "^26.5.1", | |
"ts-node-dev": "^1.1.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createTestContext } from "./__helpers"; | |
const ctx = createTestContext(); | |
it("ensures that draft can be created and published", async () => { | |
const draftResult = await ctx.client.request( | |
` | |
mutation { | |
createDraft(title: "Nexus", body: "...") { | |
id | |
title | |
body | |
published | |
} | |
} | |
` | |
); | |
expect(draftResult).toMatchSnapshot(); | |
const publishedResult = await ctx.client.request( | |
` | |
mutation publishDraft($draftId: Int!) { | |
publish(draftId: $draftId) { | |
id | |
title | |
body | |
published | |
} | |
} | |
`, | |
{ | |
draftId: draftResult.createDraft.id | |
} | |
); | |
expect(publishedResult).toMatchSnapshot(); | |
const persistedData = await ctx.db.post.findMany(); | |
expect(persistedData).toMatchSnapshot(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment