1

I have the same problem https://github.com/codeceptjs/CodeceptJS/issues/2573 , but it's answer with mocha: { bail: true }, doesn't work for me.

Codeceptjs runs all tests and printing an end result with successful/failed tests. But the process doesnt stop. I have to manually shutdown the process (when the last test failed) If all tests passed codeceptjs process exit normally. If a test failed the codeceptjs process just prints the error log and waits for the user to end the process. (I need to terminate it manually) Cant use on CI Script because the server must kill the process.

I tried to use afterSuite, but it works only for one suite. I have cases from different files.

require('ts-node/register');
require('puppeteer');
require('dotenv').config({ path: '.env' });



exports.config = {

    // define the `after` hook to run after all tests have completed

  plugins: {
      tryTo: {
          enabled: true
      },
      allure: {
          enabled: true,
          outputDir: './allure-results'
      },
      stepByStepReport: {
          enabled: true,
          output: './allure-results'
      },
  },

  tests: './tests/*/*.test.ts',
  globals: './globals.ts',
  helpers: {
      Puppeteer: {
          url: '',
          browser: 'chrome',
          chrome: {
              args: [
                  '--window-size=1920,1080',
                  '--no-sandbox',
                  '--no-zygote ',
                  '--disable-zero-browsers-open-for-tests',
                  '--ignore-certificate-errors',
                  '--ignore-certificate-errors-spki-list',
                  '--disable-setuid-sandbox',
              ],
              defaultViewport: null,
              ignoreHTTPSErrors: true,
          },
          waitForAction: 1000,
          waitForTimeout: 30000,
          fullPageScreenShots: true,
          show: false,
          restart: true,
          windowSize: '1920x1080',
          keepCookies: true,
          waitForNavigation: ['networkidle2', 'domcontentloaded', 'load']
      },
      REST: {
          endpoint: process.env.EMS_DOMAIN,
          defaultHeaders: {
              'Accept': 'application/json',
          },
          timeout: 20000
      }
  },
  bootstrap: null,
  mocha: {},
  rerun: {
      minSuccess:2,
      maxReruns: 2,
  },
  name: 'autotests-ems-js'
};

run with command yarn codeceptjs run

1 Answers1

0

To terminate an Express app while using it in Jest, you can follow these steps:

1-Use a separate Express server file: Instead of starting the Express server within the Jest test file, create a separate file (e.g., server.js) to start the Express server.

2-Export the server instance: In the server.js file, export the server instance after it's started. For example:

const express = require('express');
const app = express();

// Your Express app setup

const server = app.listen(3000, () => {
  console.log('Express server listening on port 3000');
});

module.exports = server;

to run tests you can import server inctance and writing afterAll hook like this:

// ...

afterAll((done) => {
    server.close(() => {
        done()
    })
})

// ...
milad shiriyan
  • 296
  • 1
  • 9