User Agents

Web browsers and everyday web tech, explained

Run Angular CLI Karma Tests with Headless Chrome in Docker

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Install required system dependencies for Chrome
  4. Install Chrome for headless testing
  5. Set working directory
  6. Copy package files and install dependencies
  7. Copy application source
  8. Build the Angular app (optional, but ensures all assets are ready)
  9. Run tests with headless Chrome
  10. Common Mistakes
  11. Tips
  12. Troubleshooting
  13. Frequently Asked Questions

Running Angular CLI Karma tests with headless Chrome in Docker enables consistent, isolated, and reproducible frontend testing across environments. This setup eliminates local machine dependencies and ensures your tests execute the same way in development, CI/CD pipelines, and staging environments. By leveraging Docker, you containerize the testing environment, including Node.js, Angular CLI, Chrome, and all required dependencies, ensuring reliability and portability. This guide walks you through configuring and running Angular unit tests using Karma and headless Chrome inside a Docker container.

What You'll Need

  • An existing Angular CLI project (generated via ng new)
  • Docker installed and running on your system
  • Basic familiarity with Docker, Angular CLI, and Karma configuration
  • Node.js and npm (used inside the container, but helpful for local development)

Step-by-Step

  1. Create a Dockerfile in your Angular project root. This file defines the container environment. Use a multi-stage build to keep the image efficient. Start with a Node.js base image that includes npm:
    FROM node:18 AS builder
    

    Install required system dependencies for Chrome

    RUN apt-get update \u0026\u0026 apt-get install -yq \n gconf-service \n libasound2 \n libatk1.0-0 \n libatk-bridge2.0-0 \n libc6 \n libcairo2 \n libcups2 \n libdbus-1-3 \n libexpat1 \n libfontconfig1 \n libgcc1 \n libgconf-2-4 \n libgdk-pixbuf2.0-0 \n libglib2.0-0 \n libgtk-3-0 \n libnspr4 \n libpango-1.0-0 \n libpangocairo-1.0-0 \n libstdc++6 \n libx11-6 \n libx11-xcb1 \n libxcb1 \n libxcomposite1 \n libxcursor1 \n libxdamage1 \n libxext6 \n libxfixes3 \n libxi6 \n libxrandr2 \n libxrender1 \n libxss1 \n libxtst6 \n ca-certificates \n fonts-liberation \n libappindicator1 \n libnss3 \n lsb-release \n xdg-utils \n wget \n \u0026\u0026 apt-get clean \n \u0026\u0026 rm -rf /var/lib/apt/lists/*

    Install Chrome for headless testing

    RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \n \u0026\u0026 sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' \n \u0026\u0026 apt-get update \n \u0026\u0026 apt-get install -y google-chrome-stable \n \u0026\u0026 rm -rf /var/lib/apt/lists/*

    Set working directory

    WORKDIR /usr/src/app

    Copy package files and install dependencies

    COPY package*.json ./ RUN npm install

    Copy application source

    COPY . .

    Build the Angular app (optional, but ensures all assets are ready)

    RUN ng build --configuration=development

    Run tests with headless Chrome

    CMD ["ng", "test", "--watch=false", "--browsers=ChromeHeadless"]
  2. Verify Karma Configuration in karma.conf.js. Ensure ChromeHeadless is available as a browser option. Add or modify the customLaunchers section:
    customLaunchers: {
      ChromeHeadless: {
        base: 'Chrome',
        flags: [
          '--headless',
          '--disable-gpu',
          '--no-sandbox',
          '--disable-setuid-sandbox',
          '--disable-dev-shm-usage',
          '--remote-debugging-port=9222',
          '--no-zygote'
        ]
      }
    },
    
    This configuration ensures Chrome runs without a GUI and avoids common Docker-related issues like shared memory limits.
  3. Update Angular CLI test command in package.json to ensure tests exit after completion. Modify the test script:
    "test": "ng test --watch=false --browsers=ChromeHeadless"
    This prevents the test runner from hanging in CI environments.
  4. Build the Docker image using the command:
    docker build -t angular-test .
    This creates an image named angular-test from your Dockerfile.
  5. Run the tests in Docker with:
    docker run --rm angular-test
    The --rm flag automatically removes the container after execution. You should see Karma start, launch ChromeHeadless, run all unit tests, and exit with a pass/fail status.
  6. Optional: Mount source code as volume for faster iteration during development. Instead of rebuilding the image each time, mount your local directory:
    docker run --rm -v $(pwd):/usr/src/app angular-test
    This allows live changes to be reflected without rebuilding, but ensure node_modules is not overridden.
  7. Integrate with CI/CD. Use this Docker image in GitHub Actions, GitLab CI, or Jenkins by referencing the built image or building it on the fly. This ensures consistent test execution across pipelines.

Common Mistakes

  • Forgetting to install Chrome dependencies in the Docker image, leading to Chrome failed to start errors.
  • Omitting --no-sandbox and --disable-setuid-sandbox flags, which are required in containerized environments.
  • Not setting --watch=false, causing tests to hang indefinitely in CI.
  • Mounting node_modules from the host, causing version or permission conflicts.
  • Using outdated Node.js or Chrome versions incompatible with Angular CLI.

Tips

  • Use multi-stage Docker builds to reduce final image size by discarding build-time dependencies.
  • Cache node_modules in CI by layering package.json copy before source copy.
  • Run chrome --version in the container to verify Chrome installation.
  • Add CHROME_PATH environment variable if Karma cannot locate Chrome binary.
  • Use --progress=false in CI to reduce log noise.

Troubleshooting

If tests fail to start or Chrome crashes:

  • Chrome not found: Ensure google-chrome-stable is installed and accessible in PATH. Run which google-chrome in the container to verify.
  • Out of memory: Add --disable-dev-shm-usage to Chrome flags to avoid shared memory issues in Docker.
  • Permission errors: Run Chrome with --no-sandbox and avoid running as root unless necessary.
  • Karma hangs: Confirm --watch=false is set and no async test never completes.
  • Missing modules: Double-check that package.json and package-lock.json are copied before npm install.

Frequently Asked Questions

Why use headless Chrome instead of PhantomJS?

PhantomJS is deprecated and does not fully support modern JavaScript. Headless Chrome provides better accuracy, performance, and compatibility with Angular applications.

Can I run tests interactively in Docker?

Yes. Use docker run -it --rm angular-test bash to enter the container and run ng test manually for debugging.

How do I see test output in CI logs?

Ensure Karma is set to report results to the console. Use --reporters=progress or junit for XML output in CI systems.

Does this work with Angular Universal or SSR?

Yes, but unit tests run in browser-like environments. Server-side rendering tests should be handled separately.

Can I use Firefox instead of Chrome?

Yes. Install Firefox in the container and configure Karma to use FirefoxHeadless. Adjust Docker dependencies accordingly.

How do I debug failing tests?

Temporarily remove --watch=false and run the container with -it to keep it alive, then inspect logs or attach a debugger.