How To Run Angular Unit Testing With Docker


The following software must be installed in your machine:

  1. NodeJS, you can download either LTS or the latest one.
  2. Angular CLI, make sure you install it as global dependency.
  3. Docker Engine, you can download it from here for mac user, or here for windows user
  4. Docker Compose

Step By Step Guide

Setup New Angular Project

  1. Create new project with angular-cli
  2. Change workdir to project’s root directory
  3. Import the following polyfills into your project (src/polyfills.ts):
    • core-js/es6/object
    • core-js/es6/function
    • core-js/es6/parse-int
    • core-js/es6/parse-float
    • core-js/es6/number
    • core-js/es6/math
    • core-js/es6/string
    • core-js/es6/date
    • core-js/es6/array
    • core-js/es6/regexp
    • core-js/es6/map
    • core-js/es6/weak-map
    • core-js/es6/set
    • intl
  4. Add phantomjs to your karma configuration file

    Why PhantomJS?

    By default angular-cli use google chrome to run unit tests, the chrome browser will be launched right after unit test command executed from shell, this behaviour is not supported by official nodejs docker image because it doesn’t have display/desktop GUI (unless you create a custom image, install chrome and Xvfb inside it which will make your image size become so big, and you happy with that :D)

Setup Docker

  1. Download official nodejs docker image
  2. Download phantomjs binary here and put it in root directory

    Avoid download for CI job

    For continuous integration you should avoid downloading the binary everytime CI job running

  3. Create Dockerfile
  4. Ignore local node_modules/
  5. Create docker-compose file
  6. Run test with docker-compose command


The test result should look like this:

Angular Test Result
Angular Test Result

Jenkins Integration

Simply add above docker-compose command into Build section and you are ready to go, but make sure you have Docker engine and Docker compose in your CI server

Why Docker?

Several advantage of using docker:

  1. No more saying “it is running on their machine but not on my machine”, as long as everybody use the same image and configuration this problem should never happen (anymore)
  2. Isolated environment. Application runtime will run inside a container, meaning that if your container crashed it won’t affected your computer (new to container? click here to explore)
  3. It is slightly similar to virtualization but more lite because no VM launched, Docker uses host OS kernel so all containers which run on a machine are sharing this “host” kernel

Download Source Code

You can download source code of this tutorial from here

Leave a Reply

Your email address will not be published. Required fields are marked *