Skip to content

Commit

Permalink
docs: add docs about environmentMatchGlobs (#2750)
Browse files Browse the repository at this point in the history
* docs: add docs about `environmentMatchGlobs`

* Update docs/guide/environment.md

Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com>

Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com>
  • Loading branch information
antfu and dammy001 committed Jan 25, 2023
1 parent 1b54b4c commit a9878c5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/config/index.md
Expand Up @@ -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`
Expand Down
18 changes: 18 additions & 0 deletions docs/guide/environment.md
Expand Up @@ -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
Expand Down

0 comments on commit a9878c5

Please sign in to comment.