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

feat: web worker support #726

Merged
merged 28 commits into from Mar 16, 2022
Merged

feat: web worker support #726

merged 28 commits into from Mar 16, 2022

Conversation

sheremet-va
Copy link
Member

@sheremet-va sheremet-va commented Feb 10, 2022

Not sure if this should be part of the core, since usual DOM APIs are enabled with envs

Maybe we can enable some kind of presets, like @vitest/frontend with happy-dom, worker by default, or just publish this as a different package

Closes #722

@sheremet-va sheremet-va marked this pull request as draft February 10, 2022 10:50
@netlify
Copy link

netlify bot commented Feb 10, 2022

✔️ Deploy Preview for vitest-dev ready!

🔨 Explore the source changes: 03117cf

🔍 Inspect the deploy log: https://app.netlify.com/sites/vitest-dev/deploys/623269d0ab01a1000849f464

😎 Browse the preview: https://deploy-preview-726--vitest-dev.netlify.app

@sheremet-va sheremet-va changed the title feat: worker support feat: inline worker support Feb 10, 2022
@sheremet-va sheremet-va marked this pull request as ready for review February 10, 2022 11:30
@sheremet-va sheremet-va changed the title feat: inline worker support feat: web worker support Feb 10, 2022
@antfu
Copy link
Member

antfu commented Feb 11, 2022

Emmm, interesting. I think I'd prefer to have it separate from the core to avoid the complexity and also could be a playground to dogfooding out programmatic/plugin API

@sheremet-va
Copy link
Member Author

sheremet-va commented Feb 14, 2022

Emmm, interesting. I think I'd prefer to have it separate from the core to avoid the complexity and also could be a playground to dogfooding out programmatic/plugin API

What do you think about something like this:

  1. Config option addons (should not clush with plugins, the best I came up with) receives array of strings
  2. These strings are paths to modules that export default a function with arguments equeal to __vitest_worker__ and config (maybe?). I wouldn't really want to expose them, but I need them to extend VitestRunner
  3. Call these addons with setupFiles

We can also just expose some kind of function like defineAddon() that receives a function with the same arguments ^
This function is called right away (should be called inside setup)

packages/web-worker/src/pure.ts Outdated Show resolved Hide resolved
packages/web-worker/src/pure.ts Outdated Show resolved Hide resolved
test/web-worker/test/init.test.ts Show resolved Hide resolved
packages/web-worker/rollup.config.js Outdated Show resolved Hide resolved
packages/vitest/src/node/index.ts Outdated Show resolved Hide resolved
packages/vitest/src/node/execute.ts Outdated Show resolved Hide resolved
packages/web-worker/src/pure.ts Outdated Show resolved Hide resolved
@sheremet-va
Copy link
Member Author

In the end I decided that using VitestRunner is quite a niche usecase, you would usually just await import something. But since I need to change the context, I decided to just use __vitest_worker__ inside web-worker.

@sheremet-va
Copy link
Member Author

I also moved runner and execute to runtime, since they are run inside that context

@sheremet-va
Copy link
Member Author

sheremet-va commented Feb 18, 2022

@poyoho I am very confused why CI is failing. It says Cannot find name '__vitest_worker__' in mocker.ts. VSCode doesn't show this error. Can you help me?

@poyoho
Copy link
Member

poyoho commented Feb 18, 2022

yes, I'm having a problem with positioning 🙈

@poyoho
Copy link
Member

poyoho commented Feb 18, 2022

when build the mock.ts no include worker.ts so do not had the __vitest_worker__ typescript define.

so @sheremet-va you can add this dts in src/index.ts will pass the ci.

import type { WorkerGlobalState } from './types'
declare global {
  let __vitest_worker__: WorkerGlobalState
}

@sheremet-va sheremet-va requested review from antfu and removed request for antfu March 9, 2022 07:34
@antfu antfu added this to the Next milestone Mar 11, 2022
@sheremet-va
Copy link
Member Author

sheremet-va commented Mar 11, 2022

@antfu it failes on build, that's what I've been trying to tell you. It failes because I export VitestRunner that uses __vitest_worker__, but other packages don't know what it is

@sheremet-va
Copy link
Member Author

sheremet-va commented Mar 13, 2022

@antfu CI is failing.

TBH this PR is taking so long just because of this change with global __vitest_worker__. We should either make every call to __vitest_worker__ with a wrapper with @ts-expect-error inside, or expose __vitest_worker__

@antfu
Copy link
Member

antfu commented Mar 14, 2022

I will handle that when we are preparing the release of v0.7

@antfu antfu enabled auto-merge (squash) March 16, 2022 22:57
@antfu antfu merged commit 7375347 into vitest-dev:main Mar 16, 2022
@antfu antfu removed this from the Next milestone May 3, 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

Successfully merging this pull request may close these issues.

Support for workers in Vite app?
4 participants