nodejs/undici

Allow asynchronous mocking replies

Geschlossen

#1.348 geöffnet am 19.04.2022

 (10 Kommentare) (6 Reaktionen) (0 zugewiesene Personen)JavaScript (444 Forks)batch import
enhancementgood first issue

Repository-Metriken

Stars
 (5.319 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 2T 10h) (82 gemergte PRs in 30 T)

Beschreibung

This would solve...

When trying to create a Undici adapter for Polly.JS I tried to use the mocking capabilities from Undici to record network requests and replay them later using stored HAR files. However, the MockInterceptor.reply method doesn't support asynchronous callbacks. So receiving the request, loading a file from the filesystem (asynchronously) (or passing through the request) and resolving with a promise doesn't work.

Sidenote: nock supports asynchronous callbacks: https://github.com/nock/nock#specifying-replies

The implementation should look like...

The MockInterceptor.reply method could accept asynchronous callbacks:

mockPool
  .intercept({
    path: () => true
  })
  .reply(async (options) => {
    const response = await fetchFromFileSystem(options)
    return mapping(response)
  })

I have also considered...

Creating my own version of MockAgent that supports async replies, but that just seemed too much work for a possibly temporary problem.

Additional context

Feature request for Undici adapter for Polly.JS Feature request for Undici support for Nock

Contributor Guide