Next.js Discord

Discord Forum

How to mock an API route

Answered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
I'm started implementing test. But how do I mock the API routes and the DB?
Answered by B33fb0n3
it depends a lot on your testing framework. For example for jest, you have several mock functions: https://jestjs.io/docs/mock-function-api. In general you use jest.fn() to mock a function.

So check your testing library and see what it offers and how to use it
View full answer

5 Replies

@Giant panda I'm started implementing test. But how do I mock the API routes and the DB?
it depends a lot on your testing framework. For example for jest, you have several mock functions: https://jestjs.io/docs/mock-function-api. In general you use jest.fn() to mock a function.

So check your testing library and see what it offers and how to use it
Answer
@Giant pandasolved?
Giant pandaOP
Yes. I use msw for api and vitest.fn()
Thanks for helping
happy to help