This repository is an example app on testing Node.js REST-APIs with Jest, Supertest & Chai that depend on an external database. Where cover two different testing strategies here. On the one hand we run the tests against a test database. On the other hand we run the tests without any database using a mocked data layer.
You can find a detailed explanation about the application's testing strategy and architecture on my blog.
- Language: Typescript
- Server: Express
- Database: Postgres
- Testing: Jest & Supertest & Chai
- CI/CD: GitHub Actions
- Containerization: Docker
Enter the following command to run the docker images:
docker-compose up
The app is available at http://localhost:3001.
Enter the following commands to start the app:
npm i
mv .env.example .env # Enter credentials here
npm run watch
The app is available at http://localhost:3000.
Tip: When running the app natively you can still use the Postgres Docker container.
You can run the tests using a database or not. Errors that occur during the tests are logged to the following file /logs/tests.log
Make sure to have a Postgres instance running before executing the tests.
npm run test:db
In this case the database layer is mocked.
npm run test:mock