Intercepting API calls with NextJS Server Components #28469
-
Hello, I need some help mocking an endpoint for some Cypress tests with Next 14. We are fetching data at the top level of a page using the async await syntax for server components. Something like this: export default async function Page({ params }: { params: { id: string } }) {
const response = await fetchData(params.id);
return <ClientComponent data={response} />
} I want to mock this fetch for a cypress test, so that we are not actually fetching it from our backend. However, this seems impossible to achieve with Is there a best practice for mocking in scenarios like this? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 1 reply
-
@Thor-Herman did you find a solution? |
Beta Was this translation helpful? Give feedback.
-
No, unfortunately not :-/
…On Thu, Feb 29, 2024, 16:51 V. Tinev ***@***.***> wrote:
@Thor-Herman <https://github.com/Thor-Herman> did you find a solution?
—
Reply to this email directly, view it on GitHub
<#28469 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKVVXL4MD5D76NKHO4PZU53YV5G7VAVCNFSM6AAAAABAJE3N4GVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4DMMZSGY4DS>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
@Thor-Herman Did you find any solution? |
Beta Was this translation helpful? Give feedback.
-
@Thor-Herman Did you find any solution on this? We are currently stuck on the same problem |
Beta Was this translation helpful? Give feedback.
-
@Thor-Herman no solutions yet? :( |
Beta Was this translation helpful? Give feedback.
-
Nope, I haven't pursued this any longer unfortunately
…On Thu, May 16, 2024, 15:50 barboramartinkova ***@***.***> wrote:
@Thor-Herman <https://github.com/Thor-Herman> no solutions yet? :(
—
Reply to this email directly, view it on GitHub
<#28469 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKVVXL3WZR3MWKX2CO276PTZCS2TTAVCNFSM6AAAAABAJE3N4GVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TINJYGUZDA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
So I have implemented a solution to this using Let's say you make server calls to some API, and you have the URL for that API stored in your API_URL=https://jsonplaceholder.typicode.com Then you make calls in the server components of your NextJS site like so: const data = await fetch(process.env.API_URL, { /* options here... */ }); So when we run Cypress, we typically spin up an instance of our NextJS site at the same time, right? So what I did is I created a unique script to run my NextJS site in conjunction with Cypress testing, where I redefine the value for the {
"scripts": {
"start": "next start";
"dev": "next dev";
+ "dev:cypress": "API_URL=http://localhost:9000 next dev"
}
} Ok so now all my server-to-server API calls are gonna be redirected to I configured MockTTP like so:
|
Beta Was this translation helpful? Give feedback.
So I have implemented a solution to this using
mockttp
.Let's say you make server calls to some API, and you have the URL for that API stored in your
.env
file like so:Then you make calls in the server components of your NextJS site like so:
So when we run Cypress, we typically spin up an instance of our NextJS site at the same time, right? So what I did is I created a unique script to run my NextJS site in conjunction with Cypress testing, where I redefine the value for the
API_URL
env; e.g.:{ "scripts": { "start": "next start"; "dev": "next dev"; + "dev:cy…