Docker 容器中的 Jest 单元测试
Jest Unit Testing in Docker Container
我正在尝试将我的测试移至我的 docker 图像构建阶段,但它似乎完全忽略了我的测试构建,并且在我构建图像时只是跳过它。
可能是什么问题?
# Base build
FROM node:16.13 AS build
RUN mkdir /app
WORKDIR /app
ADD package*.json ./
RUN npm i -g npm@6.14.15
RUN npm i --production
COPY . .
RUN npm run build
# Clean
RUN npm prune --production
RUN npm install -g node-prune
RUN node-prune
# Test
FROM build AS test
RUN npm i -g npm@6.14.15
RUN npm i -D jest typescript
RUN npm i -D ts-jest @types/jest
RUN npm i -D @shelf/jest-mongodb
RUN npx jest
# Release
FROM node:16.13-alpine
RUN mkdir /app
WORKDIR /app
COPY --from=build /app/dist ./dist/
COPY --from=build /app/node_modules ./node_modules/
COPY --from=build /app/package.json ./
CMD npm start
上面你可以看到我的 Dockerfile
我正在准备构建,然后计划进行测试,之后,我正在制作我的发布映像。
我已经研究了好几个小时了;我清除了缓存,对文件中的顺序进行了调整)但没有帮助。它一直忽略我的测试版本。
欢迎任何关于我的 Dockerfile 的提示
Docker 内部有两个不同的系统来构建图像。较新的 Docker 默认为名为 BuildKit 的较新系统。主要区别之一是两个系统处理多阶段构建的方式:旧系统只是 运行 所有构建阶段直到结束,但 BuildKit 计算出最后阶段 COPY --from=build
但是不使用 test
阶段的任何内容,只是跳过它。
我不会 运行 在 Docker 文件(因为它不会产生 运行nable 工件)或 Docker 中进行这些测试。在构建 Docker 图像之前,我可能会在基于主机的开发环境中 运行 它们:
# On the host
npm install # devDependencies include all test dependencies
npm run tests # locally
# repeat until the tests pass (without involving a container)
# Build the image _after_ the tests pass
docker build -t myapp .
docker build
也有 a --target
option to name a specific stage 所以你可以告诉 Docker “构建测试图像”,但你会立即删除它。这是我认为 Docker 作为测试不太有意义的地方 运行ner.
docker build -t delete-me --target test
docker build -t my-app .
docker rmi delete-me
您的 Docker 文件还安装了一个 MongoDB 测试助手。如果测试依赖于实时数据库而不是模拟实现,那么这里要注意的另一件事是 Docker 文件中的代码永远无法连接到其他容器。在这种情况下,您必须 运行 从其他地方进行此测试。
我正在尝试将我的测试移至我的 docker 图像构建阶段,但它似乎完全忽略了我的测试构建,并且在我构建图像时只是跳过它。
可能是什么问题?
# Base build
FROM node:16.13 AS build
RUN mkdir /app
WORKDIR /app
ADD package*.json ./
RUN npm i -g npm@6.14.15
RUN npm i --production
COPY . .
RUN npm run build
# Clean
RUN npm prune --production
RUN npm install -g node-prune
RUN node-prune
# Test
FROM build AS test
RUN npm i -g npm@6.14.15
RUN npm i -D jest typescript
RUN npm i -D ts-jest @types/jest
RUN npm i -D @shelf/jest-mongodb
RUN npx jest
# Release
FROM node:16.13-alpine
RUN mkdir /app
WORKDIR /app
COPY --from=build /app/dist ./dist/
COPY --from=build /app/node_modules ./node_modules/
COPY --from=build /app/package.json ./
CMD npm start
上面你可以看到我的 Dockerfile
我正在准备构建,然后计划进行测试,之后,我正在制作我的发布映像。
我已经研究了好几个小时了;我清除了缓存,对文件中的顺序进行了调整)但没有帮助。它一直忽略我的测试版本。
欢迎任何关于我的 Dockerfile 的提示
Docker 内部有两个不同的系统来构建图像。较新的 Docker 默认为名为 BuildKit 的较新系统。主要区别之一是两个系统处理多阶段构建的方式:旧系统只是 运行 所有构建阶段直到结束,但 BuildKit 计算出最后阶段 COPY --from=build
但是不使用 test
阶段的任何内容,只是跳过它。
我不会 运行 在 Docker 文件(因为它不会产生 运行nable 工件)或 Docker 中进行这些测试。在构建 Docker 图像之前,我可能会在基于主机的开发环境中 运行 它们:
# On the host
npm install # devDependencies include all test dependencies
npm run tests # locally
# repeat until the tests pass (without involving a container)
# Build the image _after_ the tests pass
docker build -t myapp .
docker build
也有 a --target
option to name a specific stage 所以你可以告诉 Docker “构建测试图像”,但你会立即删除它。这是我认为 Docker 作为测试不太有意义的地方 运行ner.
docker build -t delete-me --target test
docker build -t my-app .
docker rmi delete-me
您的 Docker 文件还安装了一个 MongoDB 测试助手。如果测试依赖于实时数据库而不是模拟实现,那么这里要注意的另一件事是 Docker 文件中的代码永远无法连接到其他容器。在这种情况下,您必须 运行 从其他地方进行此测试。