Run Angular CLI Karma Tests with Headless Chrome in Docker
In this article
- What You'll Need
- Step-by-Step
- Install required system dependencies for Chrome
- Install Chrome for headless testing
- Set working directory
- Copy package files and install dependencies
- Copy application source
- Build the Angular app (optional, but ensures all assets are ready)
- Run tests with headless Chrome
- Common Mistakes
- Tips
- Troubleshooting
- 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
- 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 builderInstall 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/appCopy package files and install dependencies
COPY package*.json ./ RUN npm installCopy application source
COPY . .Build the Angular app (optional, but ensures all assets are ready)
RUN ng build --configuration=developmentRun tests with headless Chrome
CMD ["ng", "test", "--watch=false", "--browsers=ChromeHeadless"] - Verify Karma Configuration in
karma.conf.js. Ensure ChromeHeadless is available as a browser option. Add or modify thecustomLauncherssection:
This configuration ensures Chrome runs without a GUI and avoids common Docker-related issues like shared memory limits.customLaunchers: { ChromeHeadless: { base: 'Chrome', flags: [ '--headless', '--disable-gpu', '--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage', '--remote-debugging-port=9222', '--no-zygote' ] } }, - Update Angular CLI test command in
package.jsonto ensure tests exit after completion. Modify thetestscript:
This prevents the test runner from hanging in CI environments."test": "ng test --watch=false --browsers=ChromeHeadless" - Build the Docker image using the command:
This creates an image nameddocker build -t angular-test .angular-testfrom your Dockerfile. - Run the tests in Docker with:
Thedocker run --rm angular-test--rmflag automatically removes the container after execution. You should see Karma start, launch ChromeHeadless, run all unit tests, and exit with a pass/fail status. - Optional: Mount source code as volume for faster iteration during development. Instead of rebuilding the image each time, mount your local directory:
This allows live changes to be reflected without rebuilding, but ensuredocker run --rm -v $(pwd):/usr/src/app angular-testnode_modulesis not overridden. - 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 starterrors. - Omitting
--no-sandboxand--disable-setuid-sandboxflags, which are required in containerized environments. - Not setting
--watch=false, causing tests to hang indefinitely in CI. - Mounting
node_modulesfrom 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_modulesin CI by layeringpackage.jsoncopy before source copy. - Run
chrome --versionin the container to verify Chrome installation. - Add
CHROME_PATHenvironment variable if Karma cannot locate Chrome binary. - Use
--progress=falsein CI to reduce log noise.
Troubleshooting
If tests fail to start or Chrome crashes:
- Chrome not found: Ensure
google-chrome-stableis installed and accessible inPATH. Runwhich google-chromein the container to verify. - Out of memory: Add
--disable-dev-shm-usageto Chrome flags to avoid shared memory issues in Docker. - Permission errors: Run Chrome with
--no-sandboxand avoid running as root unless necessary. - Karma hangs: Confirm
--watch=falseis set and no async test never completes. - Missing modules: Double-check that
package.jsonandpackage-lock.jsonare copied beforenpm 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.