Skip to content

Fix end() not called when app is started manually #651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

nunofgs
Copy link

@nunofgs nunofgs commented May 15, 2020

Supertest supports passing previously-started apps, ie:

const app = express();
const server = app.listen();

...

await request(server)
  .get('/user')
  .expect(200);

However, when used this way, the server was never called with end().

Note: tests are not passing but not due to this change (already failing in master).

Fixes #634

@titanism
Copy link
Collaborator

titanism commented Jul 7, 2025

🎉 This should now be resolved, please upgrade to the latest versions of supertest and superagent:

🔋 Maintenance supported by Forward Email (@forwardemail) at https://forwardemail.net

@jonathansamines
Copy link

jonathansamines commented Jul 7, 2025

Note: I realize the change was not actually introduced here, but commenting here because this issue is referenced in the release notes.

Hey, just wanted to provide some feedback on this change. we had a few test suites which broke when we updated to the latest supertest version. The reason of this failure was that our test suites were designed this way:

let server;

beforeAll(async () => {
  server = app.listen(0);
  await events.once(server, 'listening');
});

afterAll(async () => {
  if (server) {
    await util.promisify(server.close.bind(server)();
  }
});

// a few tests that use server

Previously supertest would not automatically close our tests, because of that we designed our tests to manually close the server. When we upgraded to latest supertest (a patch version), suddenly our test suite always failed with:

⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯
 FAIL  test/someTest.js > test name
Error: Server is not running.
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed | 6 passed (7)
      Tests  74 passed (74)
   Start at  14:45:40
   Duration  2.42s (transform 196ms, setup 0ms, collect 2.19s, tests 1.66s, environment 2ms, prepare 3.70s)

 ELIFECYCLE  Command failed with exit code 1.

We tracked down that failure to this change. In my opinion, supertest should not close down a server that it does not own, but if we truly want to change this behavior, then I would suggest to clearly document it as a change in behavior, and ideally released as a breaking change instead.

@titanism
Copy link
Collaborator

titanism commented Jul 7, 2025

You are correct, let me fix.

titanism added a commit that referenced this pull request Jul 7, 2025

Verified

This commit was signed with the committer’s verified signature.
@titanism
Copy link
Collaborator

titanism commented Jul 7, 2025

Fixed. All tests are passing https://github.com/forwardemail/supertest/releases/tag/v7.1.3

I've already deprecated v7.1.2 as well since it had that backwards incompatible issue and was a patch bump not major

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Jest --detectleaks shows memory leak while using SuperTest with Express
3 participants