From a9878c515f231d9d6806d3f677f6c577381dd518 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 25 Jan 2023 16:08:48 +0100 Subject: [PATCH] docs: add docs about `environmentMatchGlobs` (#2750) * docs: add docs about `environmentMatchGlobs` * Update docs/guide/environment.md Co-authored-by: Anjorin Damilare Co-authored-by: Anjorin Damilare --- docs/config/index.md | 25 +++++++++++++++++++++++++ docs/guide/environment.md | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/docs/config/index.md b/docs/config/index.md index 44490944558b..4df279c59dac 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -334,6 +334,31 @@ Vitest also exposes `builtinEnvironments` through `vitest/environments` entry, i These options are passed down to `setup` method of current [`environment`](#environment). By default, you can configure only JSDOM options, if you are using it as your test environment. +### environmentMatchGlobs + +- **Type:** `[string, EnvironmentName][]` +- **Default:** `[]` + +Automatically assign environment based on globs. The first match will be used. + +For example: + +```ts +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + environmentMatchGlobs: [ + // all tests in tests/dom will run in jsdom + ['tests/dom/**', 'jsdom'], + // all tests in tests/ with .edge.test.ts will run in edge-runtime + ['**\/*.edge.test.ts', 'edge-runtime'], + // ... + ] + } +}) +``` + ### update - **Type:** `boolean` diff --git a/docs/guide/environment.md b/docs/guide/environment.md index db145f4ac934..f60cce33c766 100644 --- a/docs/guide/environment.md +++ b/docs/guide/environment.md @@ -9,6 +9,24 @@ By default, you can use these environments: - `happy-dom` emulates browser environment by providing Browser API, and considered to be faster than jsdom, but lacks some API, uses [`happy-dom`](https://github.com/capricorn86/happy-dom) package - `edge-runtime` emulates Vercel's [edge-runtime](https://edge-runtime.vercel.app/), uses [`@edge-runtime/vm`](https://www.npmjs.com/package/@edge-runtime/vm) package +## Environments for specific files + +When setting `environment` option in your config, it will apply to all the test files in your project. To have more fine-grained control, you can use control comments to specify environment for specific files. Control comments are comments that start with `@vitest-environment` and are followed by the environment name: + +```ts +// @vitest-environment jsdom + +import { test } from 'vitest' + +test('test', () => { + expect(typeof window).not.toBe('undefined') +}) +``` + +Or you can also set [`environmentMatchGlobs`](https://vitest.dev/config/#environmentmatchglobs) option specifying the environment based on the glob patterns. + +## Custom Environment + 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`: ```ts