Skip to content

Fix server-side testing with vitest/ssr #217

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

benoitv-code
Copy link

@benoitv-code benoitv-code commented Jun 14, 2025

Fix server-side testing with vitest/ssr

Initial error

Initial error when running vitest in ssr mode, since [email protected] (introduced by #173, reported by #211):

Error: renderToString is not supported in the browser, returning undefined
    at throwInBrowser (.../node_modules/solid-js/web/dist/dev.js:1089:15)

Proposed fix

  • This PR proposes not adding 'browser' if options.ssr is set

I am not familiar with the code, and I am not sure if it could lead to issues for other setups.
An alternative could be to only add 'browser' if the conditions don't already include 'node':

-...(isTestMode && !opts.isSsrTargetWebworker ? ['browser'] : []),
+...(isTestMode && !opts.isSsrTargetWebworker && !config.resolve.conditions.includes('node') ? ['browser'] : []),

Let me know which solution seems to be the best.

Additional note

The opts.isSsrTargetWebworker check seems to be inconsistant in the original code, I've left it untouched as I am not sure what is the intent there:

// Original code: Uses `client` conditions if isSsrTargetWebworker is set
if (config.consumer === 'client' || name === 'client' || opts.isSsrTargetWebworker) {

// Original  code: Later, not using `browser` condition if isSsrTargetWebworker set
...(isTestMode && !opts.isSsrTargetWebworker ? ['browser'] : []),

Can original author comment on if the original logic seems consistent? (cc @bluwy @brenelz)

Thanks!


Fixes #211

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Testing ssr (renderToString) in vitest?
1 participant