Skip to content

Files

Latest commit

 

History

History

testing-express

Testing Express

This example shows how to implement integration tests using Express, Supertest and Prisma Client. It is based on a SQLite database, you can find the database file with some dummy data at ./prisma/dev.db.

Getting started

1. Download example and navigate into the project directory

Download this example:

npx try-prisma@latest --template orm/testing-express

Then, navigate into the project directory:

cd testing-express
Alternative: Clone the entire repo

Clone this repository:

git clone git@github.com:prisma/prisma-examples.git --depth=1

Install npm dependencies:

cd prisma-examples/orm/testing-express
npm install

[Optional] Switch database to Prisma Postgres

This example uses a local SQLite database by default. If you want to use to Prisma Postgres, follow these instructions (otherwise, skip to the next step):

  1. Set up a new Prisma Postgres instance in the Prisma Data Platform Console and copy the database connection URL.

  2. Update the datasource block to use postgresql as the provider and paste the database connection URL as the value for url:

    datasource db {
      provider = "postgresql"
      url      = "prisma+postgres://accelerate.prisma-data.net/?api_key=ey...."
    }

    Note: In production environments, we recommend that you set your connection URL via an environment variable, e.g. using a .env file.

  3. Install the Prisma Accelerate extension:

    npm install @prisma/extension-accelerate
    
  4. Add the Accelerate extension to the PrismaClient instance:

    + import { withAccelerate } from "@prisma/extension-accelerate"
    
    + const prisma = new PrismaClient().$extends(withAccelerate())

That's it, your project is now configured to use Prisma Postgres!

2. Create and seed the database

Run the following command to create your database. This also creates the User and Post tables that are defined in prisma/schema.prisma:

npx prisma migrate dev --name init

When npx prisma migrate dev is executed against a newly created database, seeding is also triggered. The seed file in prisma/seed.ts will be executed and your database will be populated with the sample data.

If you switched to Prisma Postgres in the previous step, you need to trigger seeding manually (because Prisma Postgres already created an empty database instance for you, so seeding isn't triggered):

npx prisma db seed

3. Start the REST API server

Rename the .env.example to .env and execute this command to start the server:

npm run dev

The server is now running on http://localhost:3000. You can send the API requests implemented in index.js, e.g. http://localhost:3000/feed.

4. Testing the endpoints

The tests are located in the tests folder. In these you will find tests handled for cases if a same user is added twice and also to check if the users added are obtained correctly.

The tests can be run using:

npm test

Using the REST API

You can access the REST API of the server using the following endpoints:

GET

  • /user: Fetch all users

POST

  • /user: Create a new user
    • Body:
      • email: String (required): The email address of the user
      • name: String (optional): The name of the user

Switch to another database (e.g. PostgreSQL, MySQL, SQL Server, MongoDB)

If you want to try this example with another database than SQLite, you can adjust the the database connection in prisma/schema.prisma by reconfiguring the datasource block.

Learn more about the different connection configurations in the docs.

Expand for an overview of example configurations with different databases

PostgreSQL

For PostgreSQL, the connection URL has the following structure:

datasource db {
  provider = "postgresql"
  url      = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"
}

Here is an example connection string with a local PostgreSQL database:

datasource db {
  provider = "postgresql"
  url      = "postgresql://janedoe:mypassword@localhost:5432/notesapi?schema=public"
}

MySQL

For MySQL, the connection URL has the following structure:

datasource db {
  provider = "mysql"
  url      = "mysql://USER:PASSWORD@HOST:PORT/DATABASE"
}

Here is an example connection string with a local MySQL database:

datasource db {
  provider = "mysql"
  url      = "mysql://janedoe:mypassword@localhost:3306/notesapi"
}

Microsoft SQL Server

Here is an example connection string with a local Microsoft SQL Server database:

datasource db {
  provider = "sqlserver"
  url      = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"
}

MongoDB

Here is an example connection string with a local MongoDB database:

datasource db {
  provider = "mongodb"
  url      = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority"
}

Next steps