nodejs / nodejs/undici

Open handles detected in Jest when using MockAgent

Open
#1,614 11 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug Status: help-wanted
Dominant language
JavaScript
Stars
7.7k
Forks
880
Avg merge
2d 16h
Merged PRs (30d)
68

Description

Bug Description

When testing using MockAgent in Jest, open handles are detected.

Reproducible By

I have created a minimal reproduction repository here: undici-open-handles. The README will provide all the guidance you need to reproduce the issue.

Expected Behavior

Having made a request using the MockAgent, and closed the MockAgent, no handles should be left open.

Logs & Screenshots

If you run the reproduction repository, you'll have the full logs, but here is a sample:

       9 |   client.intercept({ path: '/foo' }).reply(200, 'mockResponse')
      10 |
    > 11 |   const result = await request('http://test.com/foo').then((x) => x.body.text())
         |                               ^
      12 |
      13 |   expect(result).toBe('mockResponse')
      14 |

      at new RequestHandler (node_modules/undici/lib/api/api-request.js:38:7)
      at MockAgent.request (node_modules/undici/lib/api/api-request.js:193:25)
      at node_modules/undici/lib/api/api-request.js:186:15
      at MockAgent.request (node_modules/undici/lib/api/api-request.js:185:12)
      at node_modules/undici/index.js:76:15
      at repro.spec.ts:11:31
      at repro.spec.ts:8:71
      at Object.<anonymous>.__awaiter (repro.spec.ts:4:12)
      at Object.<anonymous> (repro.spec.ts:3:51)

Environment

OS macOS Monterey version 12.4
Node v18.0.0
npm 8.6.0
Additional context

I attempted to investigate the implementation of MockAgent, and dig into the stack trace provided by Jest's detectOpenHandles. The issue seems to be to do with the AsyncResource extended by RequestHandler not getting cleaned up correctly. But I am not familiar with how AsyncResource should be used and I can't immediately see what the issue is.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the README and repro.spec.ts in the linked undici-open-handles repository, then trace MockAgent and RequestHandler through lib/api/api-request.js. Investigate how the AsyncResource lifecycle is handled after a request and MockAgent.close(). Done means the reproduction completes without Jest detecting open handles.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api, backend, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.