Skip to content

luizcurti/node-e2e-tests

Repository files navigation

Node.js E2E and Unit Tests

REST API built with Node.js, Express, and Prisma ORM featuring a full CRUD implementation for lessons, covered by both unit tests and end-to-end tests.

Technologies

  • Node.js + TypeScript
  • Express
  • Prisma ORM
  • PostgreSQL
  • Jest + Supertest
  • Docker Compose
  • ESLint

Architecture

The project follows the Repository Pattern with Dependency Injection:

  • src/services/ — Business logic (one class per use case)
  • src/repositories/ — Repository interface contracts
  • src/repositories/prisma/ — Prisma implementations
  • test/repositories/ — In-memory implementations for unit tests
  • prisma/ — Schema, migrations, and E2E test environment

API Endpoints

Method Route Description
POST /lessons Create a lesson
GET /lessons List all lessons
GET /lessons/:id Get a lesson by ID
PUT /lessons/:id Update a lesson
DELETE /lessons/:id Delete a lesson

Request / Response Examples

POST /lessons

// Request body
{ "title": "Introduction to Node.js", "description": "Optional description" }

// Response 201
{ "id": "uuid", "title": "Introduction to Node.js", "description": "Optional description" }

PUT /lessons/:id

// Request body
{ "title": "Updated title", "description": "Updated description" }

// Response 200
{ "id": "uuid", "title": "Updated title", "description": "Updated description" }

Setup

Prerequisites

  • Node.js 18+
  • Docker and Docker Compose

1. Clone the repository

git clone https://github.com/luizcurti/node-e2e-tests.git
cd node-e2e-tests

2. Configure environment variables

Create a .env file at the project root:

DATABASE_USER=postgres
DATABASE_PASS=postgres
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_NAME=lessons
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/lessons

3. Start the database

docker compose up -d

4. Install dependencies

npm install

5. Run Prisma migrations

npx prisma migrate deploy

Running

# Development
npm run dev

# Build
npm run build

# Production
npm start

Tests

# Unit tests
npm test

# End-to-end tests (requires Docker running)
npm run test:e2e

# Lint
npm run lint

Unit Tests

Each service has its own spec file covering happy paths and error cases:

  • CreateLesson.spec.ts — title validation, trim whitespace
  • ListLessons.spec.ts — empty list, multiple items
  • GetLesson.spec.ts — found by ID, not found
  • UpdateLesson.spec.ts — update, trim, not found, empty title
  • DeleteLesson.spec.ts — delete, not found

E2E Tests

The E2E environment (prisma/prisma-test-environment.ts) creates an isolated PostgreSQL schema for each test run and drops it automatically on teardown, ensuring test isolation without affecting the development database.

About

This repository contains the code for end-to-end (E2E) testing and unit testing.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors