I'm working on a project which uses middy and we have a custom middleware set up to validate auth tokens sent with Auth0. As part of this Auth0 makes an external request. I've been using Nock to mock these requests. What I'm finding is that if I test a function that is not wrapped in middy()
, Nock works as expected and intercepts the request. However, if I try to test the function wrapped in middy()
( e.g. to ensure that all middlewares are being applied correctly ) then it fails. I've set up a very very basic example here. It just tests two functions, exactly the same, one wrapped and one not wrapped. Notice how as soon as the function is wrapped, something happens which causes Nock to not apply the interceptor correctly. I'm not sure if this is an issue with middy or Nock. I opened an issue with middy but have so far heard nothing.
Any help is greatly appreciated!
I'm not sure if this is your core issue or just in your example repo, but the reason your test fails in your repo is because you only tell Nock to mock one request and then make two separate attempts.
I can got the tests to pass by adding a call to persist
, or twice
, or skipping the first test, or creating a whole other Nock Interceptor.
READ THIS! - About interceptors
When you setup an interceptor for a URL and that interceptor is used, it is removed from the interceptor list. This means that you can intercept 2 or more calls to the same URL and return different things on each of them. It also means that you must setup one interceptor for each request you are going to have, otherwise nock will throw an error because that URL was not present in the interceptor list. If you don’t want interceptors to be removed as they are used, you can use the .persist() method.
If that doesn't solve a deeper problem for you, and you're having issues trying to determine why Nock is not matching a request, I recommend using the debug option.