Run vitest to test API routes
Hey, I tried to run vitest to test public API routes.
I tried a few attempts at creating a dedicated
vitest.config.ts
file using various plugins (tanstack-router, tanstack-start-server-fn) but none proved useful.
Digging a bit, I suppose it lies in the abstraction provided by TanStack Start's defineConfig
.
Here is my repro : https://github.com/potb/tanstack-start-vitest-repro/tree/mainGitHub
GitHub - potb/tanstack-start-vitest-repro: Created with StackBlitz ...
Created with StackBlitz ⚡️. Contribute to potb/tanstack-start-vitest-repro development by creating an account on GitHub.
4 Replies
correct-apricotOP•8mo ago
@Tanner Linsley
Coming back to this, haven’t made any progress.
Are we going to be able to call a cloudflare-like http handler to perform tests ?
i.e. pass a request object and receive a response object ?
I really like this approach since it does not involve running a http server.
Also @jesh since you reacted to the thread, what are you interested in ?
secure-lavender•8mo ago
I'm not sure yet
Some of that might depend on vite 6 and the APIs you use.
When we move off of Vinxi to just Vite + Nitro, this will become more clear
correct-apricotOP•8mo ago
I see
Clearly not a blocker, I can simply start the server and perform requests on my api routes
Thanks for the heads up
extended-salmon•8mo ago
Hi, first of all let me say thanks for this project. The approach of the Start project has been addressing many of the items I wanted in a full stack. I should also mention that I have invested less than an hour in tracing down my issues so dont take it too seriously.
I wanted to run vitest for unit tests. When I attempted to run tests importing files with
createServerFn
calls. I get the following error:
Error: Invariant failed: createServerFn must be called with a function that is marked with the 'use server' pragma. Are you using the @tanstack/start-vite-plugin ?I tried to chase down the appropriate config following the
defineConfig
function and I got lost in the little time I spent. I was also aware that you are moving away from vinxi and hoped that transition would make the new implementation more obvious and I would address then.