Adding a database
Course: Testing in the MERN Stack - chapter 7
There are two approaches to take when you add external services to your backend.
You can
- Mock out the external service calls (unit tests).
- Start the external services when the tests are running and stop them after the tests end (integration/end to end tests)
- Add prisma to your codebase
npm i prismanpx prisma init- Add a basic schema in
schema.prisma
model Sum { id Int @id @default(autoincrement()) a Int b Int result Int}- Generate the client (notice we don’t need to migrate since we wont actually need a DB)
npx prisma generate- Create
src/db.tswhich exports the prisma client. This is needed because we will be mocking this file out eventually
import { PrismaClient } from "@prisma/client";export const prismaClient = new PrismaClient();- Update
src/index.tsto store the requests in the db
$1b- Notice how the tests begin to error out now
