Companion code to the "These microservices are made for testing, and testing is what we'll do" talk.
You can find the talk slides here
This package is a microservice, complete with testing, that gives CRUD functionality to a "tenants" database.
Note! This package uses Node's ESM support, so Node v13 is the minimum version
- To install and build it, run:
npm ci
npm run build # Builds the docker image
npm test # Tests the microservice
- To run it as a microservice in a docker container
# run postgres
docker run --name postgres -d -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -p 5432:5432 postgres:12.1
# run the microservice
docker run -d -e "DATABASE_CONNECTION_STRING=postgresql://user:password@postgres:5432/postgres" giltayar/microservices-are-made-for-testing
- Alternatively, it can be run outside of the docker container,
using
./scripts/run-microservices-are-made-for-testing.js
(ornpm start
), and the environment variables as defined below.
# run postgres
docker run -d -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -p 5432:5432 postgres:12.1
export DATABASE_CONNECTION_STRING=postgresql://user:password@localhost:5432/postgres
npm start
- Docker: you need it to run the tests, but not necessarily in production
- Postgres: you don't need to install Postgres to run the tests (because the tests run the Postgres docker container by themselves), but you will need it in a production environment.
const createApp = require('@giltayar/microservices-are-made-for-testing')
// configuration options aee the same as the above corresponding environment variables
const app = createApp({databaseConnectionString: '...'})
// app is a fastify app. Use listen to start it in the usual way for [fastify](https://fastify.io)
await app.listen(/*...*/)
You can see an example of such use in test/commons/setup.js
.
To build and test it:
npm ci
npm run build # Builds the docker image
npm test # Tests the microservice
Tests can be found in the test
folder, where there are three folders:
unit
: for the unit testsit
: for the integration tests, where I run the app usingrequire
and HTTP against it to check ite2e
: for the "e2e" tests, where I run the app using its docker container and HTTP against it to check it
To publish the microservice, do:
npm publish
This will publish both the NPM package and the microservice.