问题
嘿,我没有经常使用 Docker - 我正在尝试通过 Dockerfile 运行我的 Jest 测试。但是,我得到了这个 error
尝试构建图像时:
错误
Step 13/16 : RUN if [ "$runTests" = "True" ]; then RUN npm test; fi
---> Running in ccdb3f89fb79
/bin/sh: RUN: not found
文件FROM node:10-alpine as builder
ARG TOKEN
WORKDIR /app
ARG runTests
COPY .npmrc-pipeline .npmrc
COPY package*.json ./
RUN npm install
COPY . .
RUN rm -f .npmrc
ENV PORT=2000
ENV NODE_ENV=production
RUN if [ "$runTests" = "True" ]; then \
RUN npm test; fi
RUN npm run build
EXPOSE 2000
CMD ["npm", "start"]
我用来构建图像的命令是这样的,这个想法是只有在 runTests=True 时才能运行测试。docker build -t d-image --build-arg runTests="True" --build-arg "MY TOOOOOKEN"
仅使用 Dockerfile 就可以做到这一点吗?或者是否也需要使用 docker-compose ?条件语句似乎运行良好。
不可能有两个 CMD 命令
我已尝试将此作为解决方法(但它不起作用):
Dockerfile
FROM node:10-alpine as builder
ARG TOKEN
WORKDIR /app
ARG runTests
COPY .npmrc-pipeline .npmrc
COPY package*.json ./
RUN npm install
COPY . .
RUN rm -f .npmrc
ENV PORT=3000
ENV NODE_ENV=production
RUN npm run build
EXPOSE 3000
CMD if [ "$runTests" = "True" ]; then \
CMD ["npm", "test"] && ["npm", "start"] ;fi
现在我没有从测试中得到任何输出,但它看起来很成功。进步
我已经取得了一些进展,并且在构建镜像时测试实际上正在运行。我还决定使用 RUN 命令来运行测试,以便它们在构建步骤中运行。
Dockerfile:
FROM node:10-alpine as builder
ARG TOKEN
WORKDIR /app
COPY .npmrc-pipeline .npmrc
COPY package*.json ./
RUN npm install
COPY . .
RUN rm -f .npmrc
ENV PORT=3000
ENV NODE_ENV=production
RUN npm run build
RUN npm test
EXPOSE 3000
错误:FAIL src/pages/errorpage/tests/accessroles.test.jsx
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
在我看来,docker build
进程不使用 jest:{...}
我的 package.json
中的配置,即使它是复制并安装在 Dockerfile 中的任何想法?
最佳答案
RUN
和 CMD
不是命令,它们是告诉 Docker 在构建容器时要做什么的指令。所以例如:
RUN if [ "$runTests" = "True" ]; then \
RUN npm test; fi
没有意义,RUN <command>
运行一个 shell 命令,但 RUN
没有在 shell 中定义,它应该是:ARG runTests # you need to define the argument too
RUN if [ "$runTests" = "True" ]; then \
npm test; fi
更简洁的方法是设置
npm
作为入口点,和 start
作为具体命令:ENTRYPOINT [ "npm" ]
CMD [ "start" ]
这允许您正常构建容器,它不需要任何构建参数,然后运行除 start
之外的 NPM 脚本。在容器中,例如运行 npm test
:docker run <image> test
但是,请注意,这意味着所有开发依赖项都需要在容器中。看起来(来自
ENV NODE_ENV=production
)您打算将其作为生产版本,因此您根本不应该在容器中运行测试。尽管有 as builder
这并不是真正的多阶段构建。用于此的惯用脚本将类似于:# stage 1: copy the source and build the app
FROM node:10-alpine as builder
ARG TOKEN
WORKDIR /app
COPY .npmrc-pipeline .npmrc
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# stage 2: copy the output and run it in production
FROM node:10-alpine
WORKDIR /app
ENV PORT=3000
ENV NODE_ENV=production
COPY --from=builder /app/package*.json ./
RUN npm ci
COPY --from=builder /* your build output */
EXPOSE 3000
ENTRYPOINT [ "npm" ]
CMD [ "start" ]
见例如this Dockerfile我为一个全栈 React/Express 应用程序组合在一起。
关于reactjs - 使用 Dockerfile 运行 Jest 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64966088/