Search code examples
dockerdockerfilechromiumalpine-linuxkarma-webpack

Docker (node:8.15-alpine) + Chromium + Karma unit tests not working


I'm trying to run my frontend tests on Docker (node:8.15-alpine) using Chromium and Karma, however I'm getting lots of ChromeHeadless related errors.

These tests used to work but recently they suddenly stopped, so I'm guessing it's either related to a third-party dependency (apk?), or the local Docker install.

I've created a branch on the repo with an easy to run command which reproduces the issue. README.md here: https://github.com/olivercaine/react-redux-starter-kit-extended/tree/bug/cant-run-unit-tests-in-docker

Any help with this would be masively appreciated!

Thanks.

Expected Outcome:

[output of passed tests...]
Finished in 0.026 secs / 0.031 secs @ 19:53:44 GMT+1100 (AEDT)
SUMMARY:
✔ 46 tests completed

Actual Outcome:

13 02 2020 09:10:45.314:ERROR [launcher]: Cannot start ChromeHeadless

13 02 2020 09:10:45.316:ERROR [launcher]: ChromeHeadless stdout:
13 02 2020 09:10:45.317:ERROR [launcher]: ChromeHeadless stderr:
13 02 2020 09:10:45.772:ERROR [launcher]: Cannot start ChromeHeadless

13 02 2020 09:10:45.772:ERROR [launcher]: ChromeHeadless stdout:
13 02 2020 09:10:45.773:ERROR [launcher]: ChromeHeadless stderr:
13 02 2020 09:10:45.939:ERROR [launcher]: Cannot start ChromeHeadless

13 02 2020 09:10:45.939:ERROR [launcher]: ChromeHeadless stdout:
13 02 2020 09:10:45.939:ERROR [launcher]: ChromeHeadless stderr:
13 02 2020 09:10:46.424:ERROR [launcher]: ChromeHeadless failed 2 times (cannot start). Giving up.

Finished in 0 secs / 0 secs @ 09:10:46 GMT+0000 (UTC)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! react-redux-starter-kit@3.0.1 test: `cross-env NODE_ENV=test karma start build/karma.config`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the react-redux-starter-kit@3.0.1 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-02-13T09_10_46_935Z-debug.log

Update:

The above URL has been removed, however the full (now working) repo can be found here:

https://github.com/olivercaine/react-redux-starter-kit-extended/tree/modpack/latest


Solution

  • It seems there are some issues regarding the node:8-alpine image that you are using. See https://github.com/puppeteer/puppeteer/issues/379. I don't know if it's the same issue but I was able to run your tests using the node:8.15-slim as the base image of your base Dockerfile

    You'll need to update your dev Dockerfile as well:

    FROM olliecaine/base:master
    
    WORKDIR /project
    
    # Install Chrome
    RUN apt update && apt install -y chromium
    

    Ideally you would add ENV CHROME_BIN=/usr/bin/chromium to this Dockerfile, but for some reason this is not setting the variable properly.

    To check that yourself, and insert console.log(process.env.CHROME_BIN) at the beginning of your build/karma.config.js file, you will see the value is still /usr/bin/chromium-browser when running the tests.

    As a workaround, I had to insert this line at the beginning of the build/karma.config.js file:

    process.env.CHROME_BIN = '/usr/bin/chromium'
    

    Let me know if you understand why the ENV instruction is not working, I'm interested.