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

Vitest execution in a browser page #586

Closed
4 tasks done
holtwick opened this issue Jan 19, 2022 · 9 comments
Closed
4 tasks done

Vitest execution in a browser page #586

holtwick opened this issue Jan 19, 2022 · 9 comments

Comments

@holtwick
Copy link

Clear and concise description of the problem

Currently, vitest is executing test in the classic way on the node.js engine. I would like to execute my (non UI) vitest also on real browsers in their environment.

The benefit is to test for granular differences in different browsers like missing features or APIs. For example IndexedDB or Crypto related test.

Suggested solution

Reuse vitest environment to allow test execution in a web page. vite should be able to do the necessary updates quickly. Just provide a simple output like the one we get with the --ui option.

There is no need for automation regarding launching browsers etc. this could be achieved by integrating calls to the web pages by Cypress, Puppeteer, Playwright etc. if required.

Alternative

Cypress, Playwright, etc. provide their own test environment and focus on user interfaces.

Additional context

I made a tiny implementation for my own purposes: https://github.com/holtwick/zeed/blob/master/demos/tests/src/jest.ts

Validations

@sheremet-va
Copy link
Member

sheremet-va commented Jan 19, 2022

Vitest is designed to run only inside Node. That's why we have vite-node. For example, mocking is relying on it.

Our general recommendation is to use Cypress for UI testing.

@holtwick
Copy link
Author

I see. Yeah, it is probably out of scope.

@larsthorup
Copy link

@holtwick If I understand you correctly, mocha-vite-puppeteer might give you what you want, as it runs code and tests inside Puppeteer, instead of inside Node. mocha-vite-puppeteer is powered by Vite and should be just as fast as vitest. You can find a somewhat realistic demo project using mocha-vite-puppeteer here: https://github.com/larsthorup/react-redux-typescript-vite-sandbox

@holtwick
Copy link
Author

I guess this comes close to what I intended: #891

I created a kind of runtime to be loaded before running a regular behavioral test. Maybe this gist gives an idea of it: https://gist.github.com/holtwick/ec4219363808a0d066ef02fd6c405077

@AriPerkkio
Copy link
Member

Now that #891 is on-going, I think this issue could be re-opened for discussion related to topic.

How should vitest handle coverage when tests are running in the browser? Currently vitest is using nodejs's V8 API + c8. In browsers there are similar API's. For example Puppeteer seems to be utilizing Chromium's Profiler:

This would mean that coverage collection would be limited to Chromium based browsers only. Using something like nyc would be easy but not ideal now that c8 has been chosen on the nodejs side.

@sheremet-va
Copy link
Member

Introduced browser testing is very limited at the moment, and doesn't support many features (including module mocking and snapshots). And not even released :) Not sure if we should discuss it here, or open a proper roadmap issue

@bichikim
Copy link

You might want karma-vite

@jzombie
Copy link

jzombie commented Jan 19, 2023

I'm interested when browser testing becomes more generally available.

My use case is for writing isomorphic JavaScript modules that can run on Node.js as well as in the browser.

I'm currently using Karma + Jasmine to test in browsers, while just using Jasmine for Node, which gives me the benefit of only having to write the tests once, but there's still a bit of overhead that I'd like to simplify.

@lmiller1990
Copy link

If anyone is using Vite and wants browser tests, Cypress Component Testing works with your existing Vite config out of the box. https://docs.cypress.io/guides/component-testing/overview

It's aimed at component tests and UI development, but this might be useful for people in the "I am using Vite, but I want to run some tests in a browser, too".

@github-actions github-actions bot locked and limited conversation to collaborators Jun 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants