-
|
Hi everyone, I'm working on a pnpm monorepo with the following structure:
Minimal Reproducible Code@acme/common import { PropsWithChildren, useMemo, useState } from "react";
function SessionProvider({ children }: PropsWithChildren) {
const [user, setUser] = useState<TSessionContext["data"]>({
id: 1,
username: "John Doe",
});
return (
<SessionContext.Provider value={{ user, setUser }}>
{children}
</SessionContext.Provider>
);
}
export default SessionProvider;@acme/user import { useSession } from "@acme/common";
function UserProfile() {
const { user } = useSession();
return (
<div>
{user ?.username}
</div>
);
}
export default UserProfile;@acme/app import { useSession, SessionProvider } from "@acme/common";
import { UserProfile } from "@acme/user";
function TestComponent() {
const { user } = useSession();
return (
<div>
Hello {user ?.username} from test component
</div>
);
}
function App() {
return (
<SessionProvider>
<UserProfile />
<TestComponent />
</SessionProvider>
);
}
export default App;In development mode. Both test component and userprofile gets the value from useSession but in when i test this in vitest only the TestComponent gets the value from useSession() and its says the useSession cannot find the SessionProvider. What i need help with:How can i force vitest to use a single instance from @acme/common across app and @acme/user, preventing duplicate session instances? Any insights or best practice for handling shared React context in a monorepo with vitest would be greatly appreciated! 🚀 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
|
You likely need |
Beta Was this translation helpful? Give feedback.
I took a look and the issue is mostly same as #7591.
Vitest's cjs in workspace doesn't work out of the box and, in your case, it might be simpler to add your cjs packages to
deps.externalhttps://stackblitz.com/edit/github-k8n7qkao?file=packages%2Fapp%2Fvitest.config.tsThe alternative is doing the opposite and force mjs to be picked up https://stackblitz.com/edit/github-k8n7qkao-8rcrspsz?file=packages%2Fapp%2Fvitest.config.ts