Pavlo NN
Pavlo NN

Reputation: 75

neo4j Node js Unit test with Jest

I use the Jest framework to create unit tests. When I run them. there is the message at the end:

"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."

To exit, I use the command "--forceExit". Also, I've tried to find an issue with --detectOpenHandles, but it didn't show anything.

I can't find what it hasn't closed, session or driver, or something else. How could it be fixed?

const neo4j = require("neo4j-driver");
const driver = neo4j.v1.driver(
  `bolt://${host}`,
  neo4j.v1.auth.basic(username, password)
);

beforeAll(async () => {
  await cleanDB();
});

afterAll(async () => {
  await cleanDB();
  driver.close();
});

async function cleanDB() {
  await runQuery(`...query`);
}

async function runQuery(query) {
  const session = driver.session();
  return session
    .writeTransaction(tx => tx.run(query))
    .then(result => {
      session.close();
      return result;
    })
    .catch(error => {
      session.close();
      return { error };
    });
}

describe(`bla-bla-bla`, function() {
  beforeAll(async () => {
    await dataBaseLoader(data);
  });

  test(`bla-bla-bla`, async function() {
    const result = await runQuery(
    '...query' );
   //Body of Test
    expect(result).toStrictEqual(expected);
  });

Upvotes: 2

Views: 539

Answers (1)

There is no need to use async before function if you don't use await in body, also if function is not async access it without await

function cleanDB() {
  runQuery(`...query`);
}

function runQuery(query) {
  const session = driver.session();
  return session
    .writeTransaction(tx => tx.run(query))
    .then(result => {
      session.close();
      return result;
    })
    .catch(error => {
      session.close();
      return { error };
    });
}

and so on, check all your functions, maybe it will help

Upvotes: 1

Related Questions