question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow asynchronous mocking replies

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
austinkellehercommented, Apr 25, 2022

Here is a WIP branch: https://github.com/austinkelleher/undici/commit/5e331e8c036cc37ed659aa8ec850a48ae0f42040

Here is the remaining work:

  • Consider additional edge error cases
  • Add more tests
  • Update TypeScript typings
  • Add documentation

I’m leaving early tomorrow morning for about a week. I’ll pick this back up when I get back unless someone else does first! 😅

1reaction
austinkellehercommented, Apr 25, 2022

Worked on this over the weekend and have a WIP branch. I’m supposed to leave for vacation tomorrow, so I’ll try to at least get a draft PR up today 😅.

Our team is heavily invested in Polly.JS and definitely interested in pursuing a custom Undici adapter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Moq to mock an asynchronous method for a unit test
I've devised a way around this for my unit tests by creating an IHttpClient interface and implementing a version that I use in...
Read more >
Mocking async service which makes a callback - Google Groups
We have an async service that is kicked off via an initiate endpoint, this itself is easy to mock, as it just returns...
Read more >
Mocking asynchronous functions with Jest - SwC
Mocking is a fundamental skill in testing. It allows you to avoid testing parts of your code that are outside your control, or...
Read more >
How to Mock Asynchronous Methods with Jest | by Ben Morrison
Let's look at an example. Say we have a Node application that contains a lib directory, and within that directory is a file...
Read more >
Mock Asynchronous Methods Using Moq in C# - Code Maze
Let's start it. The Repository Project We Are Going to Test. This project MockAsynchrounousMethods.Repository represents a Repository ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found