Skip to content
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

Jest did not exit one second after the test run has completed. #962

Closed
scottsymm opened this issue Jan 16, 2024 · 3 comments
Closed

Jest did not exit one second after the test run has completed. #962

scottsymm opened this issue Jan 16, 2024 · 3 comments
Labels
bug Something isn't working released

Comments

@scottsymm
Copy link

i am looking for a nice way to unit test my route handlers.
many thanks for this package.

when using the appHandler, there seems to be an open promise after my tests complete: Jest did not exit one second after the test run has completed.

is this expected behavior, or am i perhaps missing some sort of tear down call ?

Jest --detectOpenHandles

  ●  TCPWRAP

      4 | describe('POST() route', () => {
      5 |   it('FOO', async () => {
    > 6 |     await testApiHandler({
        |                         ^
      7 |       appHandler,
      8 |       async test({ fetch }) {
      9 |         const result = await fetch({ method: 'POST', body: 'dummy-data' });

      at port (../../node_modules/.pnpm/next-test-api-route-handler@4.0.0_next@14.0.2/node_modules/next-test-api-route-handler/dist/src/index.js:121:15)
      at testApiHandler (../../node_modules/.pnpm/next-test-api-route-handler@4.0.0_next@14.0.2/node_modules/next-test-api-route-handler/dist/src/index.js:120:24)
      at Object.<anonymous> (app/match/organizations/_tests/POST.route.test.ts:6:25)

My route and test

import { NextRequest, NextResponse } from 'next/server';
export async function POST(request: NextRequest) {
  return NextResponse.json({
    received: true,
  });
import { testApiHandler } from 'next-test-api-route-handler';
import * as appHandler from '../route';

describe('POST() route', () => {
  it('test()', async () => {
    await testApiHandler({
      appHandler,
      async test({ fetch }) {
        const result = await fetch({ method: 'POST', body: 'foo' });
        await expect(result.json()).resolves.toStrictEqual({ received: true });
      },
    });
  });
});

Steps To Reproduce using next-test-api-route-handler repo

  • it seems like the repo unit tests also demonstrate it
next-test-api-route-handler on  main is 📦 4.0.2 via ⬢ v18.17.1 took 24.8s 
➜ npm test

> next-test-api-route-handler@4.0.2 test
> npm run test:unit --


> next-test-api-route-handler@4.0.2 test:unit
> NODE_OPTIONS='--no-warnings' NODE_ENV=test jest '/unit-.*\.test\.tsx?' --testPathIgnorePatterns '/dist/'

 PASS  test/unit-index-imports.test.ts
 PASS  test/unit-msw.test.ts
 PASS  test/unit-external.test.ts
 PASS  test/unit-index.test.ts
A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks. Active timers can also cause this, ensure that .unref() was called on them.

Test Suites: 4 passed, 4 total
Tests:       93 passed, 93 total
Snapshots:   0 total
Time:        1.821 s
Ran all test suites matching /\/unit-.*\.test\.tsx?/i.
@scottsymm scottsymm added the bug Something isn't working label Jan 16, 2024
@Xunnamius
Copy link
Owner

Xunnamius commented Jan 16, 2024

Thanks for this detailed report! I knew there'd be some cases I was overlooking. I'll investigate tonight.

UPDATE: Ah, I see. I couldn't replicate this with npx node@21 node_modules/.bin/jest or npx node@20 node_modules/.bin/jest but it did pop up with npx node@18 node_modules/.bin/jest. Seems the test still passes even with the hang. There is likely some node@18 related issue with no backported fix.

@Xunnamius
Copy link
Owner

Turns out later versions of Node terminate persistent server connections more optimally than Node@18 does. Explicitly calling server.closeAllConnections() after calling server.close() did the trick.

Thanks again for opening this issue, I learned from fixing this.

@Xunnamius
Copy link
Owner

🎉 This issue has been resolved in version 4.0.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Xunnamius added a commit that referenced this issue Jan 18, 2024
### [4.0.3](v4.0.2...v4.0.3) (2024-01-18)

#### 🪄 Fixes

* **src:** ensure ephemeral test servers only listen on localhost ([d7774b3](d7774b3))
* **src:** forcefully close all connections after closing test server (fixes [#962](#962)) ([d03ca21](d03ca21))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

No branches or pull requests

2 participants