Nock 不能在 Middy 包装的函数中工作

Nock not working within functions wrapped with Middy

我正在开发一个使用 middy 的项目,我们设置了一个自定义中间件来验证通过 Auth0 发送的身份验证令牌。作为此 Auth0 的一部分,发出外部请求。我一直在使用 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,但到目前为止什么也没听到。

非常感谢任何帮助!

我不确定这是你的核心问题还是只是在你的示例回购中,但你的回购中的测试失败的原因是因为你只告诉 Nock 模拟一个请求然后进行两次单独的尝试。

我可以通过添加对 persisttwice 的调用,或者跳过第一个测试,或者创建一个完整的其他 Nock 拦截器来让测试通过。

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.

如果这不能为您解决更深层次的问题,并且您在尝试确定 Nock 不匹配请求的原因时遇到问题,我建议使用 debug option