Sign inSign up

edoulamenace/angular

By edoulamenace

•Updated over 6 years ago

Run angular e2e/karma tests in a Docker container

Image
0

382

edoulamenace/angular repository overview

⁠edoulamenace/angular

Run angular karma/e2e tests in a docker container. I struggled a lot to make Angular CI pipeline work. Hope my solution helps someone!

⁠Software versions

Works for me with the following software versions:

  • Angular CLI: 9.1.11
  • Node: 13.14.0
  • OS: linux x64
  • Google Chrome: 83.0.4103.97
  • protractor: 5.4.3
  • karma: 5.0.0
  • karma-source-map-support: 1.4.0
  • karma-chrome-launcher: 3.1.0
  • karma-coverage-istanbul-reporter: 2.1.0
  • karma-jasmine-html-reporter: 1.4.2
  • karma-jasmine: 3.0.1
  • karma-source-map-support: 1.4.0

⁠Usage

⁠Karma

You must add the following to your karma.config.js file:

module.exports = function (config) {
  config.set({
    // ...
    browsers: ['Chrome', 'ChromeDocker'],
    customLaunchers: {
      ChromeDocker: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },
  // ...
  });
};

Then, you can run your tests (inside the Docker container), using the command:

ng test --watch=false --browsers=ChromeDocker
⁠Protractor

There is no special configuration needed for protractorjs to work inside the container.

⁠Dockerfile

If you wish to change customize the image (change nodejs or chrome version), here is the Dockerfile:

FROM node:13.14.0
ENV CHROME_PACKAGE="google-chrome-stable_83.0.4103.97-1_amd64.deb"


WORKDIR /tmp

# Install dependencies
RUN echo "deb http://ftp.debian.org/debian stretch-backports main" >> /etc/apt/sources.list
RUN apt-get update -y
RUN apt-get install -y wget sudo
RUN apt-get install -y -t stretch-backports openjdk-8-jre libfontconfig1

# Install Chrome
RUN wget https://github.com/webnicer/chrome-downloads/raw/master/x64.deb/${CHROME_PACKAGE}
RUN dpkg --unpack ${CHROME_PACKAGE}
RUN apt-get install -f -y
RUN apt-get clean -y
RUN rm -rf /var/lib/apt/list
RUN rm ${CHROME_PACKAGE}

# Fix for the issue with Selenium, as described here:
# https://github.com/SeleniumHQ/docker-selenium/issues/87
ENV DBUS_SESSION_BUS_ADDRESS=/dev/null SCREEN_RES=1280x1024x24
RUN echo "Defaults env_keep += \"DBUS_SESSION_BUS_ADDRESS\"" > /etc/sudoers.d/environment

# For protractor
RUN mv /usr/bin/google-chrome /usr/bin/google-chrome-bin
RUN echo "#!/bin/bash" > /usr/bin/google-chrome
RUN echo "/usr/bin/google-chrome-bin --headless --no-sandbox \"\$@\"" >> /usr/bin/google-chrome
RUN chmod +x /usr/bin/google-chrome

# For karma
ENV CHROME_BIN "/opt/google/chrome/chrome"

# Workspace
WORKDIR /home/root/

⁠Note

Many people discuss of the security treat caused by using chrome with the "--no-sandbox" flag. It is indeed not secure in your own working environment. But the Docker container being itself a sandbox, i believe this configuration is fine on a development server which doesn't map volumes or ports to the real system. Be careful though, this is just a personal reflexion made by someone who doesn't know much what he is talking about. Make your own research if you are doing something serious!

Tag summary

Content type

Image

Digest

Size

635.1 MB

Last updated

over 6 years ago

docker pull edoulamenace/angular