如何在 Docker 容器内运行单元测试

Pan*_*los 5 docker karma-jasmine angular

我创建了一个运行我的 angular 项目的 docker 容器,现在我试图在容器内运行我的单元测试失败。我需要一个无头浏览器来运行我的测试,而 PhantomJS 对我的口味来说太有问题了,在运行测试时,Chrome 也会给出不同的结果。

在这里,我提供了我的 Dockerfile:

# download (or use if it's in cache) the latest official image from node
FROM node:latest

# create directory in the container and set all privileges
RUN mkdir -p /usr/src/app && chmod 777 /usr/src/app

# make the directory available for following commands
WORKDIR /usr/src/app

# copy all local's frontend content to the WORKDIR
COPY . /usr/src/app

# Expose the port the app runs in
EXPOSE 4200

CMD ["npm", "start"]
Run Code Online (Sandbox Code Playgroud)

我尝试使用 Headless Chrome,但它仍然需要一些我不知道该怎么做的更多配置。有人有任何想法吗?

Pan*_*los 3

经过大量调查后,我找到了一种方法来做到这一点:

我在前端 Dockerfile 中安装了 Chrome:

RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo 'deb http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list
RUN apt-get update && apt-get install --no-install-recommends -y google-chrome-stable
Run Code Online (Sandbox Code Playgroud)

我使用无头 Chrome 进行测试,并在 karma.config 中进行了正确的配置:

browsers: ['Chrome_without_sandbox'],
customLaunchers: {
  Chrome_without_sandbox: {
    base: 'ChromeHeadless',
    flags: ['--no-sandbox'] // with sandbox it fails under Docker
  }
},
Run Code Online (Sandbox Code Playgroud)