question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"globalTeardown" and "globalSetup" process behavior not as expected

See original GitHub issue

Do you want to request a feature or report a bug? Somewhere between feature enhancement and a bug.

What is the current behavior? Scenario A. You pass a globalSetup script in your config the result is:

  • Tests run fine
  • Watch works
  • Regular test run never “finishes”/process never exits (an issue with things that automatically run tests)

Scenario B. You pass a globalSetup and globalTeardown scripts, with the teardown running a process.exit() when it 's done, the result is:

  • Test run fine
  • Watch kills itself
  • Regular test run finishes if you manually process.exit()

Scenario C. You pass a globalSetup and globalTeardown scripts, with the teardown not running a process.exit() when it 's done - resulting in the same behavior as Scenario A.

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

Believe you can reproduce with any globalSetup/Teardown scripts - mine are async - here they are for reference: globalSetup

const Sequelize = require('sequelize');
const Umzug = require('umzug');
const path = require('path');
const env = 'test';
const config = require('./src/dbConfig')[env];
const sequelize = new Sequelize(config);

const create = async () => {
  const createConfig = { ...config };

  delete createConfig.database;

  const createSequelize = new Sequelize(createConfig);

  await createSequelize.query(`CREATE DATABASE ${createSequelize.getQueryInterface().quoteIdentifier(config.database)}`, {
    type: createSequelize.QueryTypes.RAW
  }).catch(e => console.error(e));
};

const migrate = async () => {
  const umzug = new Umzug({
    storage: 'sequelize',
    storageOptions: {
      sequelize
    },
    migrations: {
      params: [
        sequelize.getQueryInterface(),
        Sequelize
      ],
      path: path.join(__dirname, './src/migrations')
    }
  });

  await umzug.up();
};

const seed = async () => {
  const umzug = new Umzug({
    storage: 'sequelize',
    storageOptions: {
      sequelize
    },
    migrations: {
      params: [
        sequelize.getQueryInterface(),
        Sequelize
      ],
      path: path.join(__dirname, './src/testSeeders')
    }
  });

  await umzug.up();
};

module.exports = async () => {
  await create()
    .then(() => migrate()
      .then(() => seed())
    );
};

globalTeardown

const Sequelize = require('sequelize');
const env = 'test';
const config = require('./src/dbConfig')[env];

const drop = async () => {
  const dropConfig = { ...config };

  delete dropConfig.database;

  const dropSequelize = new Sequelize(dropConfig);

  await dropSequelize.query(`DROP DATABASE ${dropSequelize.getQueryInterface().quoteIdentifier(config.database)}`, {
    type: dropSequelize.QueryTypes.RAW
  }).catch(e => console.error(e));
};


module.exports = async () => {
  await drop()
    .then(() => process.exit());
};

What is the expected behavior?

Something either passed to teardown to let users of this awesome config/functionality to be able to safely exit tests or any other proposal that rectifies behavior. Or I’m an idiot and there are already things to hook into to correct this behavior - just haven’t been able to find anything after scouring the internets.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

  "jest": {
    "moduleDirectories": [
      "node_modules",
      "src"
    ],
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
      "\\.(css|less)$": "identity-obj-proxy"
    },
    "testEnvironment": "jsdom",
    "snapshotSerializers": [
      "enzyme-to-json/serializer",
      "jest-glamor-react"
    ],
    "collectCoverageFrom": [
      "src/**/*.js",
      "!src/*.js",
      "!src/state/_stubs/*",
      "!src/models/*",
      "!src/seeders/*",
      "!src/testSeeders/*",
      "!src/migrations/*"
    ],
    "coverageReporters": [
      "json-summary",
      "lcov"
    ],
    "coverageThreshold": {
      "global": {
        "lines": 10
      }
    },
    "setupTestFrameworkScriptFile": "./testSetup.js",
    "globalSetup": "./testGlobalSetup.js",
    "globalTeardown": "./testGlobalTeardown.js"
  },

jest@22.0.4 yarn@1.3.2 node@8.7.0 OSX@10.12.6

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

4reactions
SimenBcommented, Jan 30, 2018

You can use --forceExit in order to not allow Jest to wait for handles keeping the process open.

Normal errors keeping the process open are either setTimeout or setInterval without an .unref() call (if using node), or some server/db-connection not closed. I’m not sure what’s the best way to figure out what’s keeping the process open, ideas would be great! why-is-node-running used to work pretty good, but not in later versions of node.

If you find some tool capable of tracking down the open handle, please share it. Maybe we could link to it from the docs

1reaction
reidblomquistcommented, Jan 30, 2018

@SimenB Will do - might not get to it today but will get a PR up as soon as I can

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest global teardown runs before tests finish? - Stack Overflow
No, jest globalSetup and globalTeardown files don't necessarily get run in the same process as your tests. This is because jest parallelises ...
Read more >
Setup and Teardown - Jest
You have a method initializeCityDatabase() that must be called before each of these tests, and a method clearCityDatabase() that must be ...
Read more >
Configuring Jest - API Manual
Note: Any global variables that are defined through globalSetup can only be read in globalTeardown . You cannot retrieve globals defined here in...
Read more >
Mocha — Global Setup and Teardown (before/after)
A global setup hook may run before any of your tests. ... you can tell Mocha to import them sequentially and process in...
Read more >
Advanced: configuration | Playwright - CukeTest
For example, Playwright Test will always start a new worker process after a ... let globalSetup return a function that will be used...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found