TanStackT
TanStack7mo ago
3 replies
popular-magenta

Unit testing routes and middleware

How do you test things like createServerFileRoute and createMiddleware?

one example of what i'm trying to do (this code doesn't work):

import { csrfCheckMiddleware } from './csrf'; // this is a middleware i wrote with `createMiddleware`

  it('throws a 401 Unauthorized error if no Authorization header is present', async () => {
    vi.spyOn(serverUtils, 'getHeaders').mockReturnValue({});

    const next = vi.fn();

    await expect(
      csrfCheckMiddleware({ next }) //this is not callable :(
    ).rejects.toThrow(); 

    expect(next).not.toHaveBeenCalled();
  });


i looked in the tanstack start codebase and it seems like these primitives only have type-level tests currently.

does anyone have a working testing setup they can share?
Was this page helpful?