Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nuxt server environment support #531

Open
Tracked by #297
danielroe opened this issue Jun 2, 2023 · 4 comments
Open
Tracked by #297

nuxt server environment support #531

danielroe opened this issue Jun 2, 2023 · 4 comments
Labels
enhancement New feature or request vitest-environment

Comments

@danielroe
Copy link
Member

We currently support running composables/components in a browser-type environment (with happy-dom, and soon jsdom). But there are use cases where it might be useful to support running tests in a hybrid server environment where things like h3 utilities and nuxt ssr utilities work.

This could definitely be classed as non-essential and experimental, but we can track it here.

@silverbackdan
Copy link

Thanks for opening this issue. There are probably cases, such as using <ClientOnly>, but in my use case it would be simply mocking process.client and process.server for a couple of unit tests.

It seems that I can't do this in simple unit tests even for those tests which are not running this Nuxt environment once the plugin is installed.

@danielroe
Copy link
Member Author

Nuxt hard codes process.client and process.server to true/false based on environment, to enable tree-shaking. (So there is no process object that can be manipulated.)

@silverbackdan
Copy link

I see, thanks for the clarification

@rubennaatje
Copy link

rubennaatje commented Jun 28, 2023

as a work around for my apicalls / nitro plugins i added all auto imported server stuff to my setup.ts as

vitest.stubGlobal("defineEventHandler", (func: any) => func);
vitest.stubGlobal("defineNitroPlugin", (e: any) => e);
//etc

Which works absolutely fine for the time being, if anyone has a better idea/way i'd love to know!

I mock my own utilities the same way and then use them in unit tests like this

(getService as MockedFunction<typeof getService>).mockReturnValue(
  {mockedServiceCall: () => true}
);

@danielroe danielroe transferred this issue from danielroe/nuxt-vitest Dec 2, 2023
@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Apr 24, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale May 1, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label May 1, 2024
@danielroe danielroe reopened this May 1, 2024
@danielroe danielroe pinned this issue Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request vitest-environment
Projects
None yet
Development

No branches or pull requests

3 participants