mirror of
https://github.com/vitest-dev/vitest.git
synced 2025-12-08 18:26:03 +00:00
* feat: allow custom environments * chore: linting * chore: cleanup Co-authored-by: Ivan Demchuk <ivan.demchuk@gmail.com> * chore: cleanup Co-authored-by: Ivan Demchuk <ivan.demchuk@gmail.com> * chore: cleanup * docs: cleanup * docs: cleanup * Update docs/guide/environment.md Co-authored-by: Ivan Demchuk <ivan.demchuk@gmail.com> Co-authored-by: Ivan Demchuk <ivan.demchuk@gmail.com>
2.1 KiB
2.1 KiB
Test Environment
Vitest provides environment option to run code inside a specific environment. You can modify how environment behaves with environmentOptions option.
By default, you can use these environments:
nodeis default environmentjsdonemulates browser environment by providing Browser API, usesjsdompackagehappy-domemulates browser environment by providing Browser API, and considered to be faster than jsdom, but lacks some API, useshappy-dompackageedge-runtimeemulates Vercel's edge-runtime, uses@edge-runtime/vmpackage
Starting from 0.23.0, you can create your own package to extend Vitest environment. To do so, create package with the name vitest-environment-${name}. That package should export an object with the shape of Environment:
import type { Environment } from 'vitest'
export default <Environment>{
name: 'custom',
setup() {
// custom setup
return {
teardown() {
// called after all tests with this env have been run
}
}
}
}
You also have access to default Vitest environments through vitest/environments entry:
import { builtinEnvironments, populateGlobal } from 'vitest/environments'
console.log(builtinEnvironments) // { jsdom, happy-dom, node, edge-runtime }
Vitest also provides populateGlobal utility function, which can be used to move properties from object into the global namespace:
interface PopulateOptions {
// should non-class functions be bind to the global namespace
bindFunctions?: boolean
}
interface PopulateResult {
// a list of all keys that were copied, even if value doesn't exist on original object
keys: Set<string>
// a map of original object that might have been overriden with keys
// you can return these values inside `teardown` function
originals: Map<string | symbol, any>
}
export function populateGlobal(global: any, original: any, options: PopulateOptions): PopulateResult