Skip to content
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

feat!: add workspace support #3103

Merged
merged 36 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
0bd9758
feat: add workspace support
sheremet-va Mar 29, 2023
504c631
chore: enable preTransform
sheremet-va Mar 29, 2023
5fae5a0
chore: cleanup
sheremet-va Mar 29, 2023
756234c
chore: fix coverage, cleanup
sheremet-va Mar 29, 2023
90f9b3a
chore: cleanup
sheremet-va Mar 30, 2023
7b8ae2a
chore: add workspace types
sheremet-va Mar 30, 2023
2988d5c
fix: resolve browser relative to workspace
sheremet-va Mar 30, 2023
ab328a6
chore: use dirname for fallback name
sheremet-va Mar 30, 2023
b4cb35f
chore: allow changing browser provider per workspace
sheremet-va Apr 5, 2023
9a522d3
chore: fix types
sheremet-va Apr 5, 2023
b3e7cf5
chore: cleanup
sheremet-va Apr 8, 2023
e0a2813
test: fix -t in workspaces
sheremet-va Apr 8, 2023
06b7519
chore: fix browser running in a workspace
sheremet-va Apr 8, 2023
b2cfcff
refactor: move workspaces into a separate file
sheremet-va Apr 8, 2023
f85088c
chore: make workspaces public
sheremet-va Apr 8, 2023
bde49db
chore: always show workspace name
sheremet-va Apr 8, 2023
9a35fd0
test: editing file in a workspace reruns tests
sheremet-va Apr 8, 2023
dc2365c
chore: fix workspaces test
sheremet-va Apr 8, 2023
b30a416
chore: remove not used file
sheremet-va Apr 8, 2023
baab9dc
fix: allow inline workspaces
sheremet-va Apr 8, 2023
f6ab824
test: more workspaces tests
sheremet-va Apr 8, 2023
57eb347
chore: consider project name ony if specified
sheremet-va Apr 8, 2023
a959c3e
chore: cleanup
sheremet-va Apr 8, 2023
14642d6
chore: make sequencer understand workspaces
sheremet-va Apr 8, 2023
eb5a2fe
test: fix sequencer tests
sheremet-va Apr 8, 2023
04ca1c6
test: fix reporters test
sheremet-va Apr 8, 2023
b2df74f
chore: add support for json workspaces and "extends" field in workspa…
sheremet-va Apr 9, 2023
d5c9ce7
chore: add inspector to external modules
sheremet-va Apr 9, 2023
145bc8d
docs: add workspaces to feature list
sheremet-va Apr 9, 2023
2260ce7
chore: fix how tests are running in no-threads mode, respect CLI argu…
sheremet-va Apr 9, 2023
d1fc04c
refactor: rename workspace->project
sheremet-va Apr 9, 2023
964d5fb
chore: fix type errors
sheremet-va Apr 9, 2023
9ac9636
docs: more docs
sheremet-va Apr 9, 2023
b912e73
chore: add * tip to config docs
sheremet-va Apr 9, 2023
d40eb00
chore: fix docs link
sheremet-va Apr 9, 2023
f2b8c78
chore: rename workspace
sheremet-va Apr 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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