question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot exit process automatically using supertest

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:16

github_iconTop GitHub Comments

8reactions
quentinus95commented, Dec 31, 2018

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?

5reactions
LvChengbincommented, Jul 22, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest did not exit one second after the test run has completed ...
Since start.js isn't imported, the server doesn't need to be closed, Supertest request(app) sets up a server and closes it automatically.
Read more >
A step-by-step intro to end-point testing - freeCodeCamp
I've been playing around with testing lately. One thing I tried to do was to test the endpoints of my Express application.
Read more >
Supertest: How to Test APIs Like a Pro - Testim Blog
SuperTest is a Node.js testing library for HTTP APIs. Get step-by-step instructions for creating your first API test here!
Read more >
Fullstack part4 | Testing the backend
The test imports the Express application from the app.js module and wraps it with the supertest function into a so-called superagent object.
Read more >
API testing using SuperTest | HackerNoon
json() method. Then expect(200) is to check the returned status code is equal to 200. Then we end the test by calling done...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found