Skip to content

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

  1. Mock out the external service calls (unit tests).
  2. 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 prisma
npx 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.ts which 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.ts to store the requests in the db
$1b
  • Notice how the tests begin to error out now