File Explorer

/var/runtime/node_modules/@aws-sdk/node_modules/undici/docs/docs/best-practices

This explorer reads the filesystem of the server it runs on, so /workspace/user isn't present here. Browsing and the terminal still work against this server's own disk from /.

writing-tests.md2.2 KB · 64 lines
# Writing tests Undici is tuned for a production use case and its default will keepa socket open for a few seconds after an HTTP request is completed toremove the overhead of opening up a new socket. These settings that makesUndici shine in production are not a good fit for using Undici in automatedtests, as it will result in longer execution times. The following are good defaults that will keep the socket open for only 10ms: ```jsimport { request, setGlobalDispatcher, Agent } from 'undici' const agent = new Agent({  keepAliveTimeout: 10, // milliseconds  keepAliveMaxTimeout: 10 // milliseconds}) setGlobalDispatcher(agent)``` ## Guarding against unexpected disconnects Undici's `Client` automatically reconnects after a socket error. This meansa test can silently disconnect, reconnect, and still pass. Unfortunately, this could mask bugs like unexpected parser errors or protocol violations.To catch these silent reconnections, add a disconnect guard after creating a `Client`: ```jsconst { Client } = require('undici')const { test, after } = require('node:test')const { tspl } = require('@matteo.collina/tspl') test('example with disconnect guard', async (t) => {  t = tspl(t, { plan: 1 })   const client = new Client('http://localhost:3000')  after(() => client.close())   client.on('disconnect', () => {    if (!client.closed && !client.destroyed) {      t.fail('unexpected disconnect')    }  })   // ... test logic ...})``` `client.close()` and `client.destroy()` both emit `'disconnect'` events, butthose are expected. The guard only fails when a disconnect happens during theactive test (i.e., `!client.closed && !client.destroyed` is true). Skip the guard for tests where a disconnect is expected behavior, such as: - Signal aborts (`signal.emit('abort')`, `ac.abort()`)- Server-side destruction (`res.destroy()`, `req.socket.destroy()`)- Client-side body destruction mid-stream (`data.body.destroy()`)- Timeout errors (`HeadersTimeoutError`, `BodyTimeoutError`)- Successful upgrades (the socket is detached from the `Client`)- Retry/reconnect tests where the disconnect triggers the retry- HTTP parser errors from malformed responses (`HTTPParserError`)