• 23-Jan-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing Jest did not exit one second after the test run has completed in Facebook Jest

Jest did not exit one second after the test run has completed in Facebook / jest

Lightrun Team
Lightrun Team
23-Jan-2023

Explanation of the problem

Problem: When running Jest tests on an application that utilizes asynchronous operations and libraries dependent on Promises, the following message is displayed: “Jest did not exit one second after the test run has completed.” This issue occurs when Jest is unable to detect and stop the asynchronous operations being performed in the tests.

Expected Behavior: Jest should stop execution and return to the console once the test run has completed, without any error messages or issues.

Reproduction: This problem can be reproduced by implementing a function that makes a request to an external API and saves the response to a database, without waiting for the response before continuing execution. When running Jest tests on this function, the aforementioned error message will appear. The issue can also be observed by running the command “npx envinfo –preset jest” and identifying that the system is running on Windows 10 with x64 Intel(R) Core(TM) i7-7700K CPU and npm version 6.4.1.

Link to repl or repo : https://github.com/jamalsoueidan/giv-et-tilbud/commit/d8f326b6294f88d1f12136042d4adfdc83328201

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for Jest did not exit one second after the test run has completed in Facebook Jest

The issue at hand is that when using a Sequelize Model in integration tests, the database connection may not be closed properly, resulting in open handles that prevent the test code from exiting as expected. This can cause Jest to hang and not complete the test run.

To address this issue, the solution is to explicitly close the database connection in the afterAll callback. This can be done by calling the close method on the Sequelize instance that is connected to the Model. This will ensure that the connection is closed and any open handles are released, allowing Jest to exit successfully.

Here is an example of how this can be implemented in code:

// dbConnection.js
export default new Sequelize({...}); 

// some.spec.js
import dbConnection from './dbConnection';
const { SomeModel } = dbConnection.models;
describe('...', () => {
  beforeEach(async () => {
      await SomeModel.create({...});
  });
  ...
});
afterAll(async done => {
  dbConnection.close();
  done();
});

In the above example, the dbConnection.js file exports an instance of Sequelize, which is imported in the test file some.spec.js. In the beforeEach, the SomeModel is created, and in the afterAll, the dbConnection is closed and done method is called. This will close the database connection and release any open handles, allowing Jest to exit successfully.

Other popular problems with Facebook Jest

Problem: Asynchronous code not properly handled

One of the most common problems with using Jest is that it can have difficulty properly handling asynchronous code. This can lead to test failures or hanging tests. This can occur when asynchronous code is not properly handled, such as failing to return a promise or failing to call a callback function.

Solution:

To solve this problem, developers should ensure that any asynchronous code is properly handled by returning a promise or calling a callback function as appropriate. Additionally, developers can use Jest’s built-in async and await functions to handle async code, or Jest’s done() callback function to handle async code in test cases.

Problem: Mocking/stubbing of dependencies not working as expected

Another common issue with Jest is that mocking or stubbing of dependencies may not work as expected. This can occur when the mocked or stubbed function does not accurately reflect the behavior of the real function, or when the mocked or stubbed function is not called as expected.

Solution:

To resolve this issue, developers should ensure that the mocked or stubbed function accurately reflects the behavior of the real function and that the mocked or stubbed function is called as expected in the test. Developers can use Jest’s built-in mock functions, or use a third-party library such as Sinon to create more advanced mocks. Also, they should check if they are providing correct path and function name while mocking.

Problem: Tests are slow

A third common problem with Jest is that tests can be slow to run. This can occur when there are a large number of tests, or when tests are performing complex operations. This can make it difficult to quickly iterate on the code and can lead to frustration for developers.

Solution:

To address this issue, developers should aim to keep the number of tests to a minimum, and focus on testing the most important functionality. Additionally, developers can use Jest’s built-in test-running features such as the –watch option, or use a test-runner such as jest-watch-master to speed up test execution. Also, they can look into code optimization, which can be done by removing unnecessary complexity and focusing on writing performant code.

A brief introduction to Facebook Jest

Facebook Jest is a JavaScript testing framework developed and maintained by Facebook. It is a complete and easy-to-set-up JavaScript testing solution that allows developers to test their JavaScript code in a variety of environments including Node.js and web browsers. Jest is built on top of Jasmine, a popular behavior-driven development (BDD) framework, and includes features such as automatic test discovery, test-running, and code coverage analysis.

One of the key features of Jest is its powerful and flexible mocking system. This allows developers to easily mock or stub dependencies, allowing them to test their code in isolation and more effectively simulate the behavior of external systems. Jest also includes features such as built-in support for async and await, and built-in test-running features such as watch mode, which makes it easy to quickly iterate on the code during development. Jest also has built-in support for code coverage, which allows developers to see which parts of their code are being tested and identify areas that may need more tests. Additionally, Jest can be easily integrated with other popular JavaScript tools and libraries such as Babel and webpack, making it a versatile and flexible testing solution.

Most popular use cases for Facebook Jest

  1. Unit testing: Jest can be used to perform unit testing of JavaScript code. This involves testing individual units of code in isolation to ensure that they function as expected. Jest’s powerful mocking system and built-in support for async and await make it well-suited for this type of testing. An example of a unit test using Jest can be as follows:
test('adds 1 + 2 to equal 3', () => {
  expect(add(1, 2)).toBe(3);
});
  1. Integration testing: Jest can also be used to perform integration testing, which involves testing how different units of code interact with each other. Jest’s built-in test-running and code coverage features make it well-suited for this type of testing, and its support for web browsers allows developers to easily test code that runs in the browser.
  2. End-to-end testing: Jest can be used to perform end-to-end testing, which involves testing an application as a whole, from the user’s perspective. Jest’s built-in support for browser environments makes it well-suited for this type of testing, and its support for headless browsers allows developers to easily perform automated testing without the need for a graphical user interface. Additionally, Jest can be integrated with testing libraries like Puppeteer, Selenium, WebdriverIO, and so on, to perform end-to-end testing of web applications.
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications. It’s a registration form away.

Get Lightrun

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.