typescript 让Jest setup等待,直到docker compose命令执行

5vf7fwbs  于 2023-04-22  发布在  TypeScript
关注(0)|答案(1)|浏览(93)

我为Nx Nest应用程序配置了Jest E2E项目。我的目标是运行docker compose up进行E2E测试,并在测试结束后运行docker compose down
global-setup.ts中,我写道:

/* 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';
};

问题是,当我运行npx nx e2e <e2e-project>时,所有测试都因为Internal Server Error而失败,这是因为docker compose up是outran。
有什么想法吗

xlpyo6sf

xlpyo6sf1#

我强烈推荐使用https://pdmlab.github.io/docker-compose/
然后,将docker-compose.ymlglobal-setup.tsglobal-teardown.ts放在同一个目录中,像这样配置global-setup.ts

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';
};

global-teardown.ts是这样的:

/* 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__);
};

这应该能起到作用,让你开始。祝你好运,玩得开心!

相关问题