Use routes.ts
with other Vite-based tools like Vitest and Storybook
#12454
aaronadamsCA
started this conversation in
Proposals
Replies: 1 comment 1 reply
-
Hi @aaronadamsCA , // vite.config.ts
import { reactRouter } from "@react-router/dev/vite";
import { defineConfig } from "vitest/config";
import tsconfigPaths from "vite-tsconfig-paths";
export default defineConfig({
plugins: [!process.env.VITEST && reactRouter(), tsconfigPaths()],
test: {
environment: "jsdom",
css: false,
setupFiles: ["./vitest.setup.ts"],
},
}); I don't know if we can use the I can get it to work the old way // about.tsx
import { useLoaderData } from "react-router";
export function loader() {
return {
foo: "baz",
};
}
export default function Component() {
const loaderData = useLoaderData<typeof loader>();
return (
<div>
<h1>{loaderData.foo}</h1>
</div>
);
} // sandbox.test.tsx
import { createRoutesStub } from "react-router";
import { test } from "vitest";
import About from "./routes/about";
import { render, screen } from "@testing-library/react";
test("about page", async () => {
const Stub = createRoutesStub([
{
path: "/about",
Component: About,
loader() {
return {
foo: "bar",
};
},
},
]);
render(<Stub initialEntries={["/about"]} />);
await screen.findByRole("heading");
screen.debug();
}); but unfortunately not with new route typing import type { Route } from "./+types/about";
export function loader() {
return {
foo: "baz",
};
}
export default function Component({ loaderData }: Route.ComponentProps) {
return (
<div>
<h1>{loaderData.foo}</h1>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Edit: This was originally a question, but as @lhapaipai pointed out below, the Remix Vite plugin didn't work with other Vite-based tools like Vitest and Storybook. I assume that's still the case with React Router 7, so I'm rewriting this as a feature request.
I'd like to use
routes.ts
with other Vite-based tools like Vitest, Testing Library, and Storybook.Imagine a minimal app where the root route loader returns a value, and the index route component renders it. I'd like to write a test to ensure that
routes.ts
correctly integrates the two route modules.It feels like it should be this simple, and the types seem to be compatible:
But this currently fails, with an error message that doesn't make much sense:
In Remix you could do this kind of thing, but only by recreating your entire route tree in a stub. This was annoying, but it also made a certain kind of sense, since the actual Remix route manifest was an internal concern.
In React Router,
routes.ts
is a required part of every framework app. If we could create a stub from it, we could use that stub to render any path in the route tree in our tests and stories, which would be great for the developer experience.Beta Was this translation helpful? Give feedback.
All reactions