Pre-requisites of writing integration tests
Course: Integration and End to End testing - chapter 3
Before we write an integration test, we should write the code that
- Brings up the external services
- Seeds data in there
- Brings down the service when the test suite succeeds/fails
Express + prisma app
Section titled “Express + prisma app”- Initialize project
npm init -ynpx tsc --init- Update rootDir and outDir
"rootDir": "src","outDir": "dist"- Install dependencies
npm i express @types/express prisma- Initialize prisma
npx prisma init- Update schema
model Request { id Int @id @default(autoincrement()) a Int b Int answer Int type Type}
enum Type { ADD MUL}- Generate the
prisma client
npx prisma generate- Add a
db.tsfile to export the prisma client
import { PrismaClient } from "@prisma/client";
export const prismaClient = new PrismaClient();- Write the express logic (index.ts)
import express from "express";import { prismaClient } from "./db";
export const app = express();
app.use(express.json());
app.post("/sum", async (req, res) => { const a = req.body.a; const b = req.body.b;
if (a > 1000000 || b > 1000000) { return res.status(422).json({ message: "Sorry we dont support big numbers" }) } const result = a + b;
const request = await prismaClient.request.create({ data: { a: a, b: b, answer: result, type: "ADD" } })
res.json({ answer: result, id: request.id });})- Create
bin.tsto listen on a port while starting the server
import { app } from "./index";
app.listen(3000);- Try running the app locally
tsc -bnode dist/bin.jsYou will notice the request fails because we’ve not yet started the DB locally