Next.js Discord

Discord Forum

Mocking backend calls

Answered
Norwegian Lundehund posted this in #help-forum
Open in Discord
Norwegian LundehundOP
Im only using NextJS for frontend and a different backend. i want to mock the api calls to get or update or delete stuff in a (for now mock) database, what tools should i use?
jwt authentication will be used. i want the code to be able to simulate a backend and make it easy to transition to the real one once its ready. thanks!
Answered by B33fb0n3
you can create an actual database and do everything like you would do on a real DB. Call this "test DB" then something like "test_db" or similar and then work with it. After you tested everything and worked with it, you only need to change the credentials to your "real DB", call it "real_db" or something simlar and everything should work like expected automatically. Manage it easily with .env.local and .env.production.local
View full answer

14 Replies

@Norwegian Lundehund Im only using NextJS for frontend and a different backend. i want to mock the api calls to get or update or delete stuff in a (for now mock) database, what tools should i use? jwt authentication will be used. i want the code to be able to simulate a backend and make it easy to transition to the real one once its ready. thanks!
you can create an actual database and do everything like you would do on a real DB. Call this "test DB" then something like "test_db" or similar and then work with it. After you tested everything and worked with it, you only need to change the credentials to your "real DB", call it "real_db" or something simlar and everything should work like expected automatically. Manage it easily with .env.local and .env.production.local
Answer
Short mackerel
To top off the info: you can create .env variables locally and when you are hosting.
So when you are developing you point to the local DB. And when it’s deployed it automatically points to the hosted prod DB.
@Norwegian Lundehund solved?
Norwegian LundehundOP
hey, no,
so far ive worked with variable arrays that act as db tables, that i modify through a function with react query.
i dont know what tools exactly i should use and in what way to do the whole thing for it to also be good for unit testing etc
thanks
of course i can use an actual database but in this case i feel this would defeat the purpose
@Norwegian Lundehund hey, no, so far ive worked with variable arrays that act as db tables, that i modify through a function with react query. i dont know what tools exactly i should use and in what way to do the whole thing for it to also be good for unit testing etc
hmm ok. When you just want to return different data, then build your own api endpoints, that return static data. Depending on the requested data, the variables, the headers, the cookies, ... you can return different data and remove/add specific keys to the response. Like that you can mock your backend calls and work with the data
Norwegian LundehundOP
so far I've read things about msw and jest
but anyway, so far i can work with the variables and simply replace the function calls in react query with actual api calls
thanks
consider it solved
@Norwegian Lundehund so far I've read things about msw and jest
yea, jest is more for testing and not for an actual runtime env. Using msw can also be a possible way. If I would be to, I would create a simple test DB like I mentioned to easily migrate to the real DB (as you using a DB in the future as well)