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
Custom environment support #1961
Comments
We don't support jest environments, it has a different logic to how we run tests. |
Do you think it would be possible using the adapter pattern? If not, I can edit the issue to focus on supporting new Vitest environments instead. |
Currently Vitest environments are just objects:
Global argument doesn't really matter, because it's always These functions are called inside a worker, but config is resolved in the main thread, so we can't pass down any unserializable data, but we can import anything inside, so importing
|
So technically, you can do everything that you do with envs inside |
Perhaps we could support test: { ..., setupFiles: [..., `vitest-environment-${custom}`] } This is technically a breaking change because it changes which environment strings a reporter could receive in its config. This would also make it easier to support Vitest in Testing Library Recorder Extension because Chrome DevTools only lets you export a single JS file, so I can only dynamically generate Vitest config options with comments. |
I think your original proposal is fine. We would have: // vitest.config
export default {
test: {
environment: 'custom',
environmentOptions: {
custom: {} // options
}
}
}
// package "vitest-environment-custom"
import {
type EnvironmentSetup,
environments // default env if needed
populateGlobal, // copies values to global, should only really be used for browser APIs
} from 'vitest/node'
export default <EnvironmentSetup>{
name: 'custom',
setup() {
return { teardown: () => {} }
}
} |
We can also move to classes and adapter pattern, of course, so it's easier to overwrite. |
We would also need to support Edit: I opened #1962 since it also affects using built-in environments. |
So are you suggesting an API similar to Edit: I since found |
@nickmccurdy I've opened #1963. Feedback appreciated |
Clear and concise description of the problem
Jest supports running tests in custom environments using the
testEnvironment
option. I'd like to have a similar feature in Vitest, both to make it easier to migrate from Jest to Vitest, and so users of my custom environment can use Vitest. This would also make it easier to support Vitest in the Testing Library Recorder Extension, since it uses my custom environment.Suggested solution
environment
option@vitest-environment
(also@jest-environment
for compatibility)jsdom
tovite-environment-jsdom
(orjest-environment-jsdom
if we can figure out how to support Jest environments directly)Alternative
Use the adapter pattern to load Jest environments instead of supporting them directly
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: