admin管理员组文章数量:1312944
I wanted to put this question out there because I'm having trouble creating a solution for writing tests (via Jest) and mocking API requests, for a NextJS-based app. Here is a diagram that shows how the app makes requests for data. We have custom hooks that wrap @tanstack/react-query hooks that help make requests to our backend. useServiceQuery
invokes useQuery
, whose queryFn
is constructed through nextJS server actions. This was done to make queries faster as it utilizes the server to do small work like getting the configs for endpoints. Regardless, this is just to understand the context, as the actual network request for data is done on the nextJS server.
Here is where I'm having trouble understanding how to mock these functions. The easiest solution to get something in was to just mock the hooks to invoke these actions. This is its own challenge because these are useQuery/useMutation returns, so it's not ideal to create mocks because they have a state-machine associated with them (loading, pending, error, etc). It's been working easily for queries, but mutations are different because we can't just mock the return "once", we have to create a mock function for the mutate
/mutateAsync
functions as well.
I think I don't yet understand fully how I could utilize nock
or msw
as is suggested so much for mocking network requests, because network requests are done on the server in this case. So my question in the end is about how I could use jest (if at all) to mock these calls?
I wanted to put this question out there because I'm having trouble creating a solution for writing tests (via Jest) and mocking API requests, for a NextJS-based app. Here is a diagram that shows how the app makes requests for data. We have custom hooks that wrap @tanstack/react-query hooks that help make requests to our backend. useServiceQuery
invokes useQuery
, whose queryFn
is constructed through nextJS server actions. This was done to make queries faster as it utilizes the server to do small work like getting the configs for endpoints. Regardless, this is just to understand the context, as the actual network request for data is done on the nextJS server.
Here is where I'm having trouble understanding how to mock these functions. The easiest solution to get something in was to just mock the hooks to invoke these actions. This is its own challenge because these are useQuery/useMutation returns, so it's not ideal to create mocks because they have a state-machine associated with them (loading, pending, error, etc). It's been working easily for queries, but mutations are different because we can't just mock the return "once", we have to create a mock function for the mutate
/mutateAsync
functions as well.
I think I don't yet understand fully how I could utilize nock
or msw
as is suggested so much for mocking network requests, because network requests are done on the server in this case. So my question in the end is about how I could use jest (if at all) to mock these calls?
1 Answer
Reset to default 0A really handy solution is mswjs it mocks directly http calls independently of library used and with mswjs/data you can directly have a DB-like behavior
本文标签: unit testingNextJSJesthow to mock server callsStack Overflow
版权声明:本文标题:unit testing - NextJS + Jest - how to mock server calls? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741901786a2403910.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论