[英]How to setup unit test in Docker for nodejs application?
I am trying to run mocha unit test for my node application. 我正在尝试为我的节点应用程序运行mocha单元测试。 The application is built by a docker image. 该应用程序由docker镜像构建。
Docker image: Docker镜像:
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 compose: Docker撰写:
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 worked as expected. 可以按预期构建和工作设置。 The problem is not I am sure how to run unit test commands like mocha
to perform the test. 问题不是我确定如何运行像mocha
这样的单元测试命令来执行测试。
I see a module called dockunit
but I am not sure if that's the only way for now. 我看到一个名为dockunit
的模块,但我不确定这是否是目前唯一的方法。 Can anyone help me out about this? 任何人都可以帮我解决这个问题吗?
With docker
(and docker-compose
), you can run arbitrary commands in a container. 使用docker
(和docker-compose
),您可以在容器中运行任意命令。 The Dockerfile
defines the default command that is run when no other command is specified, but that doesn't mean it's the only one you can run. Dockerfile
定义了在未指定其他命令时运行的默认命令,但这并不意味着它是唯一可以运行的命令。
In your case: npm start
is run when no other command is specified. 在您的情况下:当没有指定其他命令时运行npm start
。 That happens when you do docker-compose up
. 当你做docker-compose up
时会发生这种情况。
But, you can run any command using docker run
or docker-compose run
. 但是,您可以使用docker run
或docker-compose run
运行任何命令。 For your tests, that might look like this: docker-compose run web mocha
. 对于您的测试,可能如下所示: docker-compose run web mocha
。
There is a slight difference in up
and run
, and I encourage you to read up on it: Should I use docker-compose start up or run? 有一个细微的差别up
并run
了,我鼓励你读了它: 我应该使用泊坞窗,撰写启动或运行?
Does this help you get started? 这有助于您入门吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.