Skip to content

Commit

Permalink
feat!: add workspace support (#3103)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Apr 9, 2023
1 parent ddbda10 commit b9d1a97
Show file tree
Hide file tree
Showing 83 changed files with 1,765 additions and 666 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ A blazing fast unit test framework powered by Vite.
- Components testing ([Vue](./examples/vue), [React](./examples/react), [Svelte](./examples/svelte), [Lit](./examples/lit), [Vitesse](./examples/vitesse))
- Workers multi-threading via [Tinypool](https://github.com/tinylibs/tinypool) (a lightweight fork of [Piscina](https://github.com/piscinajs/piscina))
- Benchmarking support with [Tinybench](https://github.com/tinylibs/tinybench)
- [Workspace](/guide/workspace) support
- ESM first, top level await
- Out-of-box TypeScript / JSX support
- Filtering, timeouts, concurrent for suite and tests

> Vitest requires Vite >=v3.0.0 and Node >=v14
> Vitest requires Vite >=v3.0.0 and Node >=v14.18

```ts
Expand Down
7 changes: 5 additions & 2 deletions docs/.vitepress/components.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// generated by unplugin-vue-components
// We suggest you to commit this file into source control
/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// Generated by unplugin-vue-components
// Read more: https://github.com/vuejs/core/pull/3399
import '@vue/runtime-core'

Expand All @@ -12,6 +14,7 @@ declare module '@vue/runtime-core' {
FeaturesList: typeof import('./components/FeaturesList.vue')['default']
HomePage: typeof import('./components/HomePage.vue')['default']
ListItem: typeof import('./components/ListItem.vue')['default']
NonProjectOption: typeof import('./components/NonProjectOption.vue')['default']
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
}
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/components/FeaturesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ListItem>Workers multi-threading via <a target="_blank" href="https://github.com/tinylibs/tinypool" rel="noopener noreferrer">Tinypool</a></ListItem>
<ListItem>Benchmarking support with <a target="_blank" href="https://github.com/tinylibs/tinybench" rel="noopener noreferrer">Tinybench</a></ListItem>
<ListItem>Filtering, timeouts, concurrent for suite and tests</ListItem>
<ListItem><a href="/guide/workspace">Workspace</a> support</ListItem>
<ListItem>
<a href="/guide/snapshot">
Jest-compatible Snapshot
Expand Down
10 changes: 10 additions & 0 deletions docs/.vitepress/components/NonProjectOption.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<template>
<span
text-sm
text-orange
cursor-not-allowed
title="Not supported in workspace project config"
>
*
</span>
</template>
4 changes: 4 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ export default withPwa(defineConfig({
text: 'Features',
link: '/guide/features',
},
{
text: 'Workspace',
link: '/guide/workspace',
},
{
text: 'CLI',
link: '/guide/cli',
Expand Down
3 changes: 3 additions & 0 deletions docs/.vitepress/style/vars.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
--vp-button-brand-active-border: var(--vp-c-brand-light);
--vp-button-brand-active-text: var(--vp-c-text-dark-1);
--vp-button-brand-active-bg: var(--vp-button-brand-bg);
--vp-code-tab-active-text-color: var(--vp-c-brand);
--vp-code-tab-active-bar-color: var(--vp-c-brand-darker);
--vp-code-tab-divider: var(--vp-c-brand);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions docs/api/vi.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ test('importing the next module imports mocked one', async () => {

When `partial` is `true` it will expect a `Partial<T>` as a return value.
```ts
import example from './example'
vi.mock('./example')
import example from './example.js'
vi.mock('./example.js')
test('1+1 equals 2', async () => {
vi.mocked(example.calc).mockRestore()
Expand All @@ -271,8 +271,8 @@ test('importing the next module imports mocked one', async () => {
Imports module, bypassing all checks if it should be mocked. Can be useful if you want to mock module partially.

```ts
vi.mock('./example', async () => {
const axios = await vi.importActual('./example')
vi.mock('./example.js', async () => {
const axios = await vi.importActual('./example.js')
return { ...axios, get: vi.fn() }
})
Expand Down
56 changes: 32 additions & 24 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export default mergeConfig(viteConfig, defineConfig({
In addition to the following options, you can also use any configuration option from [Vite](https://vitejs.dev/config/). For example, `define` to define global variables, or `resolve.alias` to define aliases.
:::

::: tip
All configuration options that are not supported inside a [workspace](/guide/workspace) project config have <NonProjectOption /> sign next them.
:::

### include

- **Type:** `string[]`
Expand All @@ -100,6 +104,10 @@ Handling for dependencies resolution.
- **Version:** Since Vitest 0.29.0
- **See also:** [Dep Optimization Options](https://vitejs.dev/config/dep-optimization-options.html)

::: warning
This feature is temporary disabled since Vitest 0.30.0.
:::

Enable dependency optimization. If you have a lot of tests, this might improve their performance.

For `jsdom` and `happy-dom` environments, when Vitest will encounter the external library, it will be bundled into a single file using esbuild and imported as a whole module. This is good for several reasons:
Expand Down Expand Up @@ -141,7 +149,7 @@ When a dependency is a valid ESM package, try to guess the cjs version based on

This might potentially cause some misalignment if a package has different logic in ESM and CJS mode.

#### deps.registerNodeLoader
#### deps.registerNodeLoader<NonProjectOption />

- **Type:** `boolean`
- **Default:** `false`
Expand Down Expand Up @@ -411,15 +419,15 @@ export default defineConfig({
})
```

### update
### update<NonProjectOption />

- **Type:** `boolean`
- **Default:** `false`
- **CLI:** `-u`, `--update`, `--update=false`

Update snapshot files. This will update all changed snapshots and delete obsolete ones.

### watch
### watch<NonProjectOption />

- **Type:** `boolean`
- **Default:** `true`
Expand All @@ -434,7 +442,7 @@ Enable watch mode

Project root

### reporters
### reporters<NonProjectOption />

- **Type:** `Reporter | Reporter[]`
- **Default:** `'default'`
Expand All @@ -452,7 +460,7 @@ Custom reporters for output. Reporters can be [a Reporter instance](https://gith
- `'hanging-process'` - displays a list of hanging processes, if Vitest cannot exit process safely. This might be a heavy operation, enable it only if Vitest consistently cannot exit process
- path of a custom reporter (e.g. `'./path/to/reporter.ts'`, `'@scope/reporter'`)

### outputFile
### outputFile<NonProjectOption />

- **Type:** `string | Record<string, string>`
- **CLI:** `--outputFile=<path>`, `--outputFile.json=./path`
Expand Down Expand Up @@ -485,21 +493,21 @@ Even though this option will force tests to run one after another, this option i
This might cause all sorts of issues, if you are relying on global state (frontend frameworks usually do) or your code relies on environment to be defined separately for each test. But can be a speed boost for your tests (up to 3 times faster), that don't necessarily rely on global state or can easily bypass that.
:::

### maxThreads
### maxThreads<NonProjectOption />

- **Type:** `number`
- **Default:** _available CPUs_

Maximum number of threads. You can also use `VITEST_MAX_THREADS` environment variable.

### minThreads
### minThreads<NonProjectOption />

- **Type:** `number`
- **Default:** _available CPUs_

Minimum number of threads. You can also use `VITEST_MIN_THREADS` environment variable.

### useAtomics
### useAtomics<NonProjectOption />

- **Type:** `boolean`
- **Default:** `false`
Expand All @@ -524,14 +532,14 @@ Default timeout of a test in milliseconds

Default timeout of a hook in milliseconds

### teardownTimeout
### teardownTimeout<NonProjectOption />

- **Type:** `number`
- **Default:** `10000`

Default timeout to wait for close when Vitest shuts down, in milliseconds

### silent
### silent<NonProjectOption />

- **Type:** `boolean`
- **Default:** `false`
Expand Down Expand Up @@ -591,14 +599,14 @@ Beware that the global setup is run in a different global scope, so your tests d
:::


### watchExclude
### watchExclude<NonProjectOption />

- **Type:** `string[]`
- **Default:** `['**/node_modules/**', '**/dist/**']`

Glob pattern of file paths to be ignored from triggering watch rerun.

### forceRerunTriggers
### forceRerunTriggers<NonProjectOption />

- **Type**: `string[]`
- **Default:** `['**/package.json/**', '**/vitest.config.*/**', '**/vite.config.*/**']`
Expand Down Expand Up @@ -626,7 +634,7 @@ Make sure that your files are not excluded by `watchExclude`.

Isolate environment for each test file. Does not work if you disable [`--threads`](#threads).

### coverage
### coverage<NonProjectOption />

You can use [`c8`](https://github.com/bcoe/c8), [`istanbul`](https://istanbul.js.org/) or [a custom coverage solution](/guide/coverage#custom-coverage-provider) for coverage collection.

Expand Down Expand Up @@ -703,7 +711,7 @@ List of files excluded from coverage as glob patterns.
- **Type:** `boolean`
- **Default:** `false`
- **Available for providers:** `'c8' | 'istanbul'`
- **CLI:** `--coverage.all`, --coverage.all=false`
- **CLI:** `--coverage.all`, `--coverage.all=false`

Whether to include all files, including the untested ones into report.

Expand Down Expand Up @@ -906,7 +914,7 @@ Watermarks for statements, lines, branches and functions. See [istanbul document
Specifies the module name or path for the custom coverage provider module. See [Guide - Custom Coverage Provider](/guide/coverage#custom-coverage-provider) for more information.
### testNamePattern
### testNamePattern<NonProjectOption />
- **Type** `string | RegExp`
- **CLI:** `-t <pattern>`, `--testNamePattern=<pattern>`, `--test-name-pattern=<pattern>`
Expand All @@ -928,7 +936,7 @@ test('doNotRun', () => {
})
```
### open
### open<NonProjectOption />
- **Type:** `boolean`
- **Default:** `false`
Expand Down Expand Up @@ -1091,13 +1099,13 @@ export default defineConfig({
})
```
### snapshotFormat
### snapshotFormat<NonProjectOption />
- **Type:** `PrettyFormatOptions`
Format options for snapshot testing. These options are passed down to [`pretty-format`](https://www.npmjs.com/package/pretty-format).
### resolveSnapshotPath
### resolveSnapshotPath<NonProjectOption />
- **Type**: `(testPath: string, snapExtension: string) => string`
- **Default**: stores snapshot files in `__snapshots__` directory
Expand All @@ -1122,15 +1130,15 @@ export default defineConfig({
Allow tests and suites that are marked as only.
### dangerouslyIgnoreUnhandledErrors
### dangerouslyIgnoreUnhandledErrors<NonProjectOption />
- **Type**: `boolean`
- **Default**: `false`
- **CLI:** `--dangerouslyIgnoreUnhandledErrors` `--dangerouslyIgnoreUnhandledErrors=false`
Ignore any unhandled errors that occur.
### passWithNoTests
### passWithNoTests<NonProjectOption />
- **Type**: `boolean`
- **Default**: `false`
Expand Down Expand Up @@ -1199,7 +1207,7 @@ A number of tests that are allowed to run at the same time marked with `test.con
Test above this limit will be queued to run when available slot appears.
### cache
### cache<NonProjectOption />
- **Type**: `false | { dir? }`
Expand All @@ -1224,7 +1232,7 @@ You can provide sequence options to CLI with dot notation:
npx vitest --sequence.shuffle --sequence.seed=1000
```
#### sequence.sequencer
#### sequence.sequencer<NonProjectOption />
- **Type**: `TestSequencerConstructor`
- **Default**: `BaseSequencer`
Expand All @@ -1243,7 +1251,7 @@ If you want tests to run randomly, you can enable it with this option, or CLI ar
Vitest usually uses cache to sort tests, so long running tests start earlier - this makes tests run faster. If your tests will run in random order you will lose this performance improvement, but it may be useful to track tests that accidentally depend on another run previously.
#### sequence.seed
#### sequence.seed<NonProjectOption />
- **Type**: `number`
- **Default**: `Date.now()`
Expand Down Expand Up @@ -1330,7 +1338,7 @@ By default, if Vitest finds source error, it will fail test suite.
Path to custom tsconfig, relative to the project root.
### slowTestThreshold
### slowTestThreshold<NonProjectOption />
- **Type**: `number`
- **Default**: `300`
Expand Down
36 changes: 35 additions & 1 deletion docs/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pnpm add -D vitest
```

:::tip
Vitest requires Vite >=v3.0.0 and Node >=v14
Vitest requires Vite >=v3.0.0 and Node >=v14.18
:::

It is recommended that you install a copy of `vitest` in your `package.json`, using one of the methods listed above. However, if you would prefer to run `vitest` directly, you can use `npx vitest` (the `npx` command comes with npm and Node.js).
Expand Down Expand Up @@ -61,6 +61,40 @@ export default defineConfig({

See the list of config options in the [Config Reference](../config/)

## Workspaces Support

Run different project configurations inside the same project with [Vitest Workspaces](/guide/workspace). You can define a list of files and folders that define you workspace in `vitest.workspace` file. The file supports `js`/`ts`/`json` extensions. This feature works great with monorepo setups.

```ts
import { defineWorkspace } from 'vitest/config'

export default defineWorkspace([
// you can use a list of glob patterns to define your workspaces
// Vitest expects a list of config files
// or directories where there is a config file
'packages/*',
'tests/*/vitest.config.{e2e,unit}.ts',
// you can even run the same tests,
// but with different configs in the same "vitest" process
{
test: {
name: 'happy-dom',
root: './shared_tests',
environment: 'happy-dom',
setupFiles: ['./setup.happy-dom.ts'],
},
},
{
test: {
name: 'node',
root: './shared_tests',
environment: 'node',
setupFiles: ['./setup.node.ts'],
},
},
])
```

## Command Line Interface

In a project where Vitest is installed, you can use the `vitest` binary in your npm scripts, or run it directly with `npx vitest`. Here are the default npm scripts in a scaffolded Vitest project:
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ vi.mock('pg', () => {
return { Client }
})

vi.mock('./handlers', () => {
vi.mock('./handlers.js', () => {
return {
success: vi.fn(),
failure: vi.fn(),
Expand Down
Loading

0 comments on commit b9d1a97

Please sign in to comment.