How to set up a unit test in a Docker app for nodejs?
I am trying to run a mocha unit test for my node application. The app is built with docker image.
Docker image:
FROM node:6.10.0-alpine
RUN mkdir -p /app
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
EXPOSE 3000
CMD ["npm", "start"]
Docker composes:
version: "3"
services:
web: #### nodejs image
build: .
volumes:
- ./app/
ports:
- "3000:3000"
depends_on:
- db
db:
build: ##### postgres db image
context: .
dockerfile: dbDockerfile
ports:
- 5432:5432
The setup can be built and work as expected. The problem is not that I'm sure how to execute unit test commands like mocha
to execute a test.
I see a module called dockunit
, but I'm not sure if this is the only way. Can anyone help me about this?
source to share
With docker
(and docker-compose
) you can run arbitrary commands in the container. Dockerfile
defines a default command that runs when no other command is specified, but that doesn't mean it's the only one you can run.
In your case: npm start
runs when no other command is specified. This happens when you do docker-compose up
.
But you can execute any command using docker run
or docker-compose run
. For your tests it might look like this: docker-compose run web mocha
.
In up
and run
there is a small difference, and I recommend you read this: Should I start or build dockers?
Does it help you get started?
source to share