jest: Cannot exit process automatically using supertest

Do you want to request a feature or report a bug?

A bug.

What is the current behavior?

The process doesn’t exit after running all test suites.

Use the code below:

const request = require('supertest');

describe('GET /stars', () => {
  it('respond with json', (done) => {
    const app = require('http').createServer((req, res) => {
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end('{ "stars": 1989 }');
    });
    request(app.listen())
      .get('/stars')
      .expect('Content-Type', /json/)
      .expect(200)
      .end((err, res) => {
        if (err) done(err);

        expect(res.body).toEqual({ stars: 1989 });

        done();
      });
  });
});

What is the expected behavior?

The process should exit automatically.

Please provide your exact Jest configuration

// package.json
{
  ...,
  "jest": {
      "bail": true
    },
  ...
}

Run npx envinfo --preset jest in your project directory and paste the results here

System:
    OS: macOS High Sierra 10.13.3
    CPU: x64 Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
  Binaries:
    Node: 8.10.0
    Yarn: Not Found
    npm: 5.7.1
  npmPackages:
    jest:
      wanted: ^22.4.2
      installed: 22.4.2

Actually, jest@^20.0.0 cannot work (I had tried that).

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 16 (1 by maintainers)

Most upvoted comments

Same for me, when using request(app) with a valid test case but only one Jest test file, I get the Jest did not exit one second after the test run has completed.

Otherwise, when adding an empty test in another file, it completes normally.

With this test:

describe('Fake test', () => {
  it('does nothing', async () => {
  })
})

image

Without:

image

@SimenB can you re-open this issue, as this may be related to Jest?

I found a strange behavior of Jest about this problem. If there is only one test file, such as index.spec.js, the jest will not exit if app.listen() is called. But if there are multiple test files, it will exit as expected.

When using koa (v2.7.0), this issue could be resolved by replacing app.listen() to app.callback(). For example,

const supertest = require('supertest')
const { app } = require('../src/app')

const request = supertest(app.callback())

describe('routes', () => {
  it(' GET /', () => {
    return request.get('/').expect(200)
  })
})

@quentinus95 I always add --forceExit instead, it’s not graceful but at least it works.

Thanks to this article. It helped me to fix the problem. https://www.albertgao.xyz/2017/05/24/how-to-test-expressjs-with-jest-and-supertest/

Thanks @samarthanavatti for the link - most important is to keep an app (with all routes) & a server (which runs app.listen(…)) in separate files and then include only the app in tests

You should not call .listen.

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Had the same issue when testing with travis-ci. Shut the server down correctly, destroyed all socket connections properly, still no process exiting. Tests finish locally. The solution was --maxWorkers=15 flag.