Docker container fails to execute mocha puppeteer tests

I’m trying to run my mocha test suite with puppeteer inside a docker container but it keeps failing. The error message I get is about missing shared libraries: Error: Mocha: Got error running globalSetup - /usr/src/app/node_modules/mocha-environment-puppeteer/setup.js, reason: Failed to launch the browser process! /usr/src/app/node_modules/puppeteer/.local-chromium/linux-884014/chrome-linux/chrome: error while loading shared libraries: libxshmfence.so.1: cannot open shared object file: No such file or directory

I’m pretty new to containerization so I’m not sure what’s missing. Here are my config files:

mocha.config.ts

module.exports = {
  setupFiles: ['dotenv/config'],
  preset: "mocha-puppeteer",
  roots: ['<rootDir>/tests'],
  transform: {
    '^.+\.tsx?$': 'ts-mocha'
  },
  testRegex: '(/__tests__/.*|(\.|/)(test|spec))\.tsx?$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
}

mocha-puppeteer.config.js

module.exports = {
  launch: {
    headless: true,
    defaultViewport: null,
    args: ['--start-maximized', '--disable-gpu', 
          '--disable-dev-shm-usage', '--disable-setuid-sandbox', 
          '--no-sandbox'],
  },
  testEnvironmentOptions: { resources: 'usable' },
};

Dockerfile

FROM node:16.14.0
WORKDIR /app/src
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4000
RUN chown -R node /app/src
USER node
CMD npm run test

I tried installing chrome manually in the container but that didn’t help either.

Had the same issue last month. Your base image is missing the X11 libraries Chrome needs. Skip the manual package installs and just use the official Puppeteer Docker image - it’s got everything configured already. Switch your FROM line to FROM buildkite/puppeteer:latest and remove puppeteer from your package.json since it’s baked in. Also add --no-first-run and --disable-default-apps to your launch config. Saved me hours of dependency hunting and runs way more reliably.

Yeah, it’s definitely the missing system dependencies. But here’s a better approach - don’t switch base images. Just install what puppeteer needs on your current node image. Add this after your FROM line: RUN apt-get update && apt-get install -y wget gnupg ca-certificates procps libxss1 libgconf-2-4 libxrandr2 libasound2 libpangocairo-1.0-0 libatk1.0-0 libc6-dev libdrm2 libxcomposite1 libxdamage1 libxtst6 libappindicator1 libnss3 libgcc1 libxss1. Also, move your USER node command to the very end. Running npm install as the node user screws up puppeteer’s chromium download. This has worked consistently for me across different environments.

yep, looks like you need some additional libraries for chrome. swap your base image to FROM node:16.14.0-bullseye and add this line: RUN apt-get update && apt-get install -y libxshmfence1 libgconf-2-4 libxrandr2 libxss1 libgtk-3-0 libasound2 before running npm install. it worked for me!