Jest: Cannot exit process automatically using supertest

Created on 12 Mar 2018  路  15Comments  路  Source: facebook/jest

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

Most helpful comment

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?

All 15 comments

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.

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.

I can confirm that behavior for [email protected]

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?

Also happens for me, runs fine with 2 or more test files.

  • I just found out that using --runInBand it fails even with several tests.
  • Also the code coverage is broken when not using --runInBand and using the previous hack; only coverage for the first test file is taken into account

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

To correct on my previous message, code coverage is not broken, it was a mistake on my side.

The issue with --runInBand still occurs.

@LvChengbin I don't like to use --forceExit, I would prefer find out if the issue is from Jest, Supertest or my app.

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.

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)
  })
})

@jjangga0214 it returns call back is not a function

--forceExit

The problem is that it will stop printing/debug the test

@dotku What do you mean? It's same as a single instance of koa, except it's not "listening"(port and infinite loop). Would you check the official docs if you think there's a confusion between our points
?

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timoxley picture timoxley  路  76Comments

simon360 picture simon360  路  78Comments

bookman25 picture bookman25  路  79Comments

SimenB picture SimenB  路  69Comments

udbhav picture udbhav  路  236Comments