2 years ago
#44913
sachith
Run Karma test in Docker
I have a Angular project and it has some tests. I am running this project in google cloud and it is working. Tests pass in the local environment and Karma is used. Now I want to generate some test coverage reports and send that to Sonarqube. I tried several examples in the internet and failed to run the tests using Karma in Docker. I am unable to find a way to how to setup Chrome in Docker. Here is my current configurations.
karma.conf.js
process.env.CHROME_BIN = '/usr/bin/chromium'
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, './coverage/front-end'),
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'coverage-istanbul'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['ChromeHeadlessNoSandbox'],
customLaunchers: {
ChromeHeadless: {
base: 'Chrome',
flags: ['--no-sandbox',
'--headless']
}
},
singleRun: true,
restartOnFileChange: true
});
};
Dockerfile
FROM node:12-slim as build
# Install Chrome
RUN apt update && apt install -y chromium
ENV CHROME_BIN='/usr/bin/chromium-browser'
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install -g @angular/cli@9.1.13
RUN npm install
COPY . .
RUN node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build --prod --output-path=dist
# use Angular CLI to trigger a test run so we have coverage data
RUN ./node_modules/.bin/ng test --watch=false --code-coverage
FROM nginx:1.17-alpine
COPY --from=build /usr/src/app/dist /usr/share/nginx/html
COPY nginx/default.conf /etc/nginx/conf.d/default.conf
EXPOSE 8080
Here is a part of the error message.
Step #1: 12 01 2022 11:01:44.525:INFO [launcher]: Starting browser Chrome Step #1: 12 01 2022 11:01:45.792:ERROR [launcher]: Cannot start Chrome Step #1: [94:94:0112/110144.876282:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180. Step #1: Step #1: 12 01 2022 11:01:45.793:ERROR [launcher]: Chrome stdout: Step #1: 12 01 2022 11:01:45.794:ERROR [launcher]: Chrome stderr: [94:94:0112/110144.876282:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180. Step #1: Step #1: 12 01 2022 11:01:46.071:INFO [launcher]: Trying to start Chrome again (1/2). Step #1: 12 01 2022 11:01:49.558:ERROR [launcher]: Cannot start Chrome Step #1: [105:105:0112/110146.523462:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180. Step #1: Step #1: 12 01 2022 11:01:49.558:ERROR [launcher]: Chrome stdout: Step #1: 12 01 2022 11:01:49.565:ERROR [launcher]: Chrome stderr: [105:105:0112/110146.523462:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180. Step #1: Step #1: 12 01 2022 11:01:50.335:INFO [launcher]: Trying to start Chrome again (2/2). Step #1: 12 01 2022 11:01:53.128:ERROR [launcher]: Cannot start Chrome Step #1: [116:116:0112/110150.813860:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180. Step #1: Step #1: 12 01 2022 11:01:53.128:ERROR [launcher]: Chrome stdout: Step #1: 12 01 2022 11:01:53.129:ERROR [launcher]: Chrome stderr: [116:116:0112/110150.813860:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180. Step #1: Step #1: 12 01 2022 11:01:55.535:ERROR [launcher]: Chrome failed 2 times (cannot start). Giving up. Step #1: The command '/bin/sh -c ./node_modules/.bin/ng test --watch=false --code-coverage' returned a non-zero code: 1
javascript
angular
docker
karma-runner
karma-coverage
0 Answers
Your Answer