19

I have the following simple test setup:

test('what did I do to deserve this', async () => {
  expect.assertions(1)

  const data = await fetchData() // or fetchData2 
  expect(data).toBe('peanut butter')
})

async function fetchData () {
  return "peanut butter"
}

async function fetchData2 () {
  return knex.select('name').from('foos')
}

When I use fetchData jest finishes happily.
But when I use fetchData2 it complains of this:

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue.

The data variable does have the result from the db query, and other callers higher in the API resolve the query fine and continue the execution of other statements.

I have tried:

  1. the --detectOpenHandles flag but it doesn't show me anything.
  2. making the expect pass for fetchData2 in case it was the issue described here
  3. passing a done arg to the async function in test. It does exist, but calling it does not fix the warning.
  4. throwing try/catch blocks at it

Thanks for any help on making this happy.

Versions of things:

  • Node v11.1.0
  • "jest": "^23.6.0"
  • "knex": "^0.15.2"
blu
  • 12,905
  • 20
  • 70
  • 106

3 Answers3

18

To forcefully close Jest rather than DB Connection :

--forceExit

So my test script looked something like this,

"scripts": {
        "test": "jest --forceExit"
    }
Dikshit Kathuria
  • 1,182
  • 12
  • 15
  • 7
    This works but now I'm seeing: "Force exiting Jest: Have you considered using `--detectOpenHandles` to detect async operations that kept running after all tests finished?" – zero_cool Nov 05 '20 at 23:34
12

You need to call knex.destroy() in the end of the test suite to teardown connection pool.

Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70
  • Ahh, I see this ticket now https://github.com/Vincit/objection.js/issues/534. Ty – blu Nov 12 '18 at 18:16
0

Got these errors:

1.

A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks.

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue. node:internal/process/promises:246 triggerUncaughtException(err, true /* fromPromise */); ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Error: Request failed with status code 404".] { code: 'ERR_UNHANDLED_REJECTION' } ##[error]Cmd.exe exited with code '1'.

Example with global setup and teardown:

// setup.js
const knexConfiguration = require('../config/knex')[process.env.NODE_ENV];
const knex = require('knex');

const setup = async () => {
  const client = knex(knexConfiguration)
  await client.migrate.latest();
  await client.destroy();
};

module.exports = setup;

// teardown.js
const knexConfiguration = require('../config/knex')[process.env.NODE_ENV];
const knex = require('knex');

const teardown = async () => {
  const client = knex(knexConfiguration)
  await client.migrate.rollback();
  await client.destroy();
};

module.exports = teardown;

Source:

https://github.com/facebook/jest/issues/7287#issuecomment-510068325

Another example:

dbConnection.js

export default new Sequelize({...}); // The Sequelize instance.

some.spec.js

import dbConnection from './dbConnection';

const { SomeModel } = dbConnection.models;

describe('...', () => {
  beforeEach(async () => {
      await SomeModel.create({...});
  });
  ...
});

afterAll(async done => {
  // Closing the DB connection allows Jest to exit successfully.
  dbConnection.close();
  done();
});

https://github.com/facebook/jest/issues/7287#issuecomment-434058564

Ogglas
  • 62,132
  • 37
  • 328
  • 418