1

I have Jest E2E project configured for Nx Nest app. My goal is to run docker compose up for E2E testing and docker compose down after testing is over.

In global-setup.ts, I wrote:

/* eslint-disable */
var __TEARDOWN_MESSAGE__: string;

var os = require('os');

module.exports = async function () {
  // Start services that that the app needs to run (e.g. database, docker-compose, etc.).
  console.log('\nSetting up...\n');

  globalThis.execCmd = (cmd: string) => {
    const exec = require('child_process').exec;
    const command = exec(cmd, (err, stdOut, stdErr) => {
        if (err) {
            console.log(err);
            return;
        }

        console.log(stdOut);
    });

    return command;
  };

  const isWin = os.platform() === 'win32';
  const dockerComposeUpCmd = 'docker compose up';

  await globalThis.execCmd(dockerComposeUpCmd).on('exit', (code) => {
    console.log(`${command}: Finished with code ${code}`);
  });


  // Hint: Use `globalThis` to pass variables to global teardown.
  globalThis.__TEARDOWN_MESSAGE__ = '\nTearing down...\n';
};

The problem is that, when I run npx nx e2e <e2e-project>, all tests fail because of Internal Server Error, which happens because docker compose up is outran.

Any ideas?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Arkadi
  • 1,153
  • 1
  • 14
  • 35

1 Answers1

0

I would highly recommend using https://pdmlab.github.io/docker-compose/ for this.

Then while having docker-compose.yml in the same directory as your global-setup.ts and global-teardown.ts, configure global-setup.ts like this:

import * as path from 'path';
import * as compose from 'docker-compose';

/* eslint-disable */
var __TEARDOWN_MESSAGE__: string;
var __COMPOSE__: any;

module.exports = async function () {
  // Start services that the app needs to run (e.g. database, docker-compose, etc.).
  console.log('\nSetting up...');

  globalThis.__COMPOSE__ = compose;

  try {
    await globalThis.__COMPOSE__.upAll({
      cwd: path.join(__dirname),
      commandOptions: [['--build'], ['--force-recreate']],
      callback: (chunk: Buffer) => {
        console.log('job in progress: ', chunk.toString());
      },
    });

    console.log('Boot-up completed.');
  } catch (err) {
    console.log('Something went wrong during docker compose boot-up:', err.message);
  }

  // Hint: Use `globalThis` to pass variables to global teardown.
  globalThis.__TEARDOWN_MESSAGE__ = '\nTearing down...\n';
};

while global-teardown.ts like this:

/* eslint-disable */

import path from 'path';

module.exports = async function () {
  // Put clean up logic here (e.g. stopping services, docker-compose, etc.).
  // Hint: `globalThis` is shared between setup and teardown.

  try {
    await globalThis.__COMPOSE__.down({
      cwd: path.join(__dirname),
      commandOptions: [['--volumes'], ['--remove-orphans'], ['-t', '1']],
      callback: (chunk: Buffer) => {
        console.log('job in progress: ', chunk.toString());
      },
    });

    console.log('Shutdown completed.');
  } catch (err) {
    console.log('Something went wrong during docker compose shutdown:', err.message);
  }

  console.log(globalThis.__TEARDOWN_MESSAGE__);
};

This should do the trick and get you started. Good luck and have fun!

Bartekus
  • 601
  • 1
  • 9
  • 15