Next.js Discord

Discord Forum

How to test Nextjs / server actions

Noronha posted this in #help-forum
Open in Discord
I have server actions that use drizzle ORM. My question is how to test it?
Should I test components individually? Or end-to-end tests?

Back in the day (lol) when I created rest APIs in Java I had a bunch of unit tests that runs on the endpoints.
Since is my first 'serious' project in nextjs/typescript I'm a bit lost.

Sorry if this looks like a "drizzle" question and not a next.js one.
Anyways, thank you =]

26 Replies

Eric Burel
That's actually a tough question and officiel doc mostly stick to "here's how to setup things until we have a more dedicated package"
I am writing a course to go more deeply in this area, I've opened the waitlist:
you'll want to avoid "testing the framework"
so you're not testing Drizzle but more if your actions correctly make use of Drizzle
RSCs/Server Actions are deeply integrated with data so it's hard to unit test them
you would have to play around with dependency injections to replace drizzle with a mock you can observe
other ideas are setting end-to-end testing of your Next app
basically Next turns Server Actions into POST endpoint
so you could either run Next so you can test this POST endpoint via synthetic user interaction
or test the server action directly by calling it in Jest
sorry I've opened more questions than I answered hope it helps ':)
Thanks, just joined the waiting list 🙂
Yeah I think I understand what you mean by 'dont test the framework'. In java is quite easy to setup a in-memory docker database instance and just start from there. Or even mock the DAOs like you mentioned. Maybe my app is not well structured. If I had another layer on top of Drizzle (like DAO/or Service). Maybe it would be easy to inject a mock instead. But somethings when I test stuff with mocks, feels like I'm not really testing stuff, lol it might just be me though.

But again, I have no idea on how to actually set this up so for now I might stick to an end-to-end approach.
Any recommendations?
Eric Burel
You could do some integration testing like having an actual drizzle db (don't know if it's realistic with this service?) + Jest to run the test by calling the server action as a function
it might be slow but it will be real
sometimes db have good mocks too like Mongo has an in-memory equivalent that is good and fast for testing
but it depends
I prefer integration/e2e too there, and keep unit for more algorithmic logic like if your action happens to have a zillion conditions it might be worth mocking the db and focusing on testing the function
Thank you Eric ❤️ Do you mind if I leave this thread unanswered to see if we can get other people to collaborate and give us their opinion?
Eric Burel
no problem of course!
@Eric Burel looks like we got some news here:

Didnt read it yet =] But wanted to share with you regardless
Eric Burel
yes thanks! I've seen this page I think, it's good but talks a lot about setup
recently I had the chance to study end-to-end testing and the research paper around that,
they sometimes have mitigated return on investment because of their fragility
I think a big part of the content I'll create will be focused on that
but yeah otherwise that's a great starting point for a good setup, the rest often comes naturally trying and learning and testing different libs