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

Error when running tests with Vitest #241

Closed
clemvnt opened this issue Aug 10, 2022 · 10 comments
Closed

Error when running tests with Vitest #241

clemvnt opened this issue Aug 10, 2022 · 10 comments

Comments

@clemvnt
Copy link

clemvnt commented Aug 10, 2022

Describe the bug

When I run the tests with Vitest, I get the following error:

image

I guess adding the vitest.setup.ts to the exports list (and in the files list because this file is not published) of the package.json file of the solid-start package should solve the problem. But looking at this file, I wonder if it is really required as you can do tests without needing @testing-library/jest-dom.

To fix this issue on my side, I added a custom vite plugin that override the config and more precisely the test.setupFiles option :

image

Then, I got another error :

image

To fix this error, I setted the test.deps.registerNodeLoader option to true (like in the solid example of the vitest repo) :
image

This option seems to be required since v0.21.0 (vitest-dev/vitest#1778).

And now, tests are working :

image

Reproduction

https://github.com/clemvnt/solid-stack

To reproduce the error, you must comment the custom vite plugin that override the config.

Alternative

I wonder if it is really necessary for the solid-start vite plugin to completely define the vitest configuration because it seems that we cannot add our own configuration such as :
image

Is it possible that the configuration could be overwritten on our side? The solid-start vite plugin could still set the required options (test.transformMode.web and test.deps.registerNodeLoader) and leave the possibility to add or override options.

Moreover, the test.globals, test.setupFiles, test.threads, test.isolate options do not seem to be required for the tests to work. Wouldn't it be better not to set these options to use the default vitest behavior and let the user choose?

I suppose that this alternative would require a change in the following lines :

- test: {
+ test: mergeConfig({
   environment: "jsdom",
   transformMode: {
     web: [/\.[tj]sx?$/]
   },
+  deps: {
+    registerNodeLoader: true
+  },
+ }, conf.test),
-  globals: true,
-  setupFiles: "node_modules/solid-start/vitest.setup.ts"
-  threads: false
-  isolate: false
- },

Depending on which solution you choose, fixing the missing import or the proposed alternative, I could make a pull request.

@nksaraf
Copy link
Member

nksaraf commented Aug 11, 2022

Thanks for this! I have been reconsidering this approach since I added it and basically was waiting for some feedback. This is very helpful, and im gonna think about a good solution.. the idea is I wanted for people to be the install vitest, jsdom, @testing-library/jest-dom, etc. and start testing without configuring

@atk
Copy link
Contributor

atk commented Aug 11, 2022

I have already an idea on how to do that. We can test in the running config which environment is available and adapt the config correspondingly; also we can set/augment the testing config in the plugin. I'm currently a bit low on time, but if nobody else will, I can prepare a PR.

@clemvnt
Copy link
Author

clemvnt commented Aug 21, 2022

Also, leaving the choice of environment will be even more relevant when @vitest/browser is ready.

Some people will want to run their tests directly in the browser, others will want to use jsdom (or happy-dom but not right away because there are blocking issues: capricorn86/happy-dom#544, capricorn86/happy-dom#343).

@atk
Copy link
Contributor

atk commented Oct 16, 2022

The happy-dom issue is supposedly resolved.

@cnava9389
Copy link

The hydration error can also be fixed if you add solid-js as an inline dependency in vite.config.

test: {
environment: 'happy-dom',
includeSource: ['src/**/*.{js,ts,jsx,tsx}'],
globals: true,
isolate: true,
deps: {
inline: [/solid-js/],
},
transformMode: {
web: [/.[jt]sx?$/],
}
}

@ryansolid
Copy link
Member

ryansolid commented Nov 28, 2022

@clemvnt Is this issue resolved now?

@atk
Copy link
Contributor

atk commented Nov 28, 2022

The happy-dom issues are fixed by now. Instead of deps.inline = [/solid-js/], the new proposed configuration is deps.registerNodeLoader = true.

@orenelbaum
Copy link
Contributor

It seems like the issue can be closed?
The Vitest template seems to be working fine.

@atk
Copy link
Contributor

atk commented Dec 4, 2022

I agree. This can be closed.

@clemvnt
Copy link
Author

clemvnt commented Dec 9, 2022

The issue is solved. Thank you all!

@clemvnt clemvnt closed this as completed Dec 9, 2022
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

No branches or pull requests

6 participants