Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run unit tests inside Docker container

I have created a docker container which runs my angular project and now I'm trying to run my unit tests inside the container unsuccessfully. I need a headless browser to run my tests and PhantomJS is too buggy for my taste, also gives different results with Chrome, when running tests.

Here, I provide my 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"]

I tried using Headless Chrome, but still it needs some more configuration that I don't know how to do it. Anyone having any thoughts?

like image 938
Panos Vakalopoulos Avatar asked Oct 29 '22 04:10

Panos Vakalopoulos


1 Answers

after a lot of investigation, I found a way to do this:

I installed Chrome inside my frontend Dockerfile:

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

and I used headless Chrome for my tests with the proper configuration inside karma.config:

browsers: ['Chrome_without_sandbox'],
customLaunchers: {
  Chrome_without_sandbox: {
    base: 'ChromeHeadless',
    flags: ['--no-sandbox'] // with sandbox it fails under Docker
  }
},
like image 52
Panos Vakalopoulos Avatar answered Nov 15 '22 05:11

Panos Vakalopoulos