@onerepo/plugin-vitest
Installation
Section titled Installationnpm install --save-dev @onerepo/plugin-vitest
yarn add --dev @onerepo/plugin-vitest
pnpm install --save-dev @onerepo/plugin-vitest
Vitest supports Workspaces with a little extra configuration than you may by used to. Create two files at the root of your repository: vitest.config.ts
and vitest.workspace.ts
:
-
Create a root config
This is the generic configuration for tests across all workspaces. Anything configurations here will be defaults for each Workspace.
vitest.config.ts import { defineConfig } from 'vitest/config';export default defineConfig({}); -
Create a Workspace config
In the
vitest.workspace.ts
file, we need to duplicate the workspace globs from our originalpackage.json
orpnpm-workspace.yaml
file and ensure they point to valid Vitest config files:vitest.workspace.ts import { defineWorkspace } from 'vitest/config';export default defineWorkspace(['<rootDir>/apps/*/vitest.config.ts','<rootDir>/modules/*/vitest.config.ts',]); -
Create each Workspace configs
In each Workspace that has tests, create a new Vitest config file, ensuring to use
defineProject
(notdefineConfig
):apps/my-app/vitest.config.ts import { defineProject } from 'vitest/config';export default defineProject({});
Configuration
Section titled Configurationvitest()
Section titled vitest()function vitest(opts): PluginObject;
Include the vitest
plugin in your oneRepo plugin setup:
import { vitest } from '@onerepo/plugin-vitest';
export default { plugins: [vitest()],};
Parameters
Section titled ParametersParameter | Type |
---|---|
opts | Options |
Options
Section titled Optionstype Options: { config: string; name: string | string[];};
Options for configuring the Vitest plugin for oneRepo.
export default { plugins: [ vitest({ name: ['test'], }), ],};
Type declaration
Section titled Type declarationconfig?
Section titled config?optional config: string;
Specify the main Jest configuration file, if different from <repo>/vitest.config.js
. This can be relative to the repository root.
export default { plugins: [ vitest({ config: 'configs/vitest.config.js' }), ],});
name?
Section titled name?optional name: string | string[];
- Default:
'vitest'
Rename the default command name. This configuration is recommended, but not provided, to avoid potential conflicts with other commands.
export default { plugins: [ vitest({ name: ['test', 'vitest'] }), ],});
Recommended tasks
Section titled Recommended tasksimport type { Config } from 'onerepo';
export default { tasks: { 'pre-commit': { serial: ['$0 vitest'], }, 'pre-merge': { serial: ['$0 vitest -a'], }, },} satisfies Config;
Commands
Section titled Commandsone vitest
Section titled one vitestAliases: one test
Run unit tests using Vitest
one vitest ,test [options...] -- [passthrough]one vitest [options...]
This test commad will automatically attempt to run only the test files related to the changes in your git branch. By passing specific filepaths as extra passthrough arguments after two dashes (--
), you can further restrict the tests to those specific files only.
Additionally, any other Vitest CLI options can be used as passthrough arguments as well after an argument separator (two dashes --
).
Option | Type | Description |
---|---|---|
--affected | boolean | Select all affected Workspaces. If no other inputs are chosen, this will default to true . |
--all, -a | boolean | Run across all Workspaces |
--inspect | boolean | Break for the the Node inspector to debug tests. |
--staged | boolean | Use files on the git stage to calculate affected files or Workspaces. When unset or --no-staged , changes will be calculated from the entire branch, since its fork point. |
--watch | boolean | Shortcut for vitest --watch mode. |
--workspaces, -w | array | List of Workspace names to run against |
Advanced options
Option | Type | Description |
---|---|---|
--config | string , default: "./vitest.config.ts" | Path to the vitest.config file, relative to the repo root. |
--from-ref | string | Git ref to start looking for affected files or Workspaces |
--show-advanced | boolean | Pair with --help to show advanced options. |
--through-ref | string | Git ref to start looking for affected files or Workspaces |
Run only tests related to modified files.
one vitest ,test
Run vitest in —watch mode.
one vitest ,test --watch
Run vitest in watch mode with a particular file.
one vitest ,test --watch -- path/to/test.ts
Run all tests in a given Workspace.
one vitest ,test -w <workspaces...>
How can I stop Vitest from clearing the output when running in
We’ve filed an issue with Vitest, vitest-dev/vitest#5185 in hopes of being able to prevent the screen from clearing automatically from within the oneRepo plugin. However, until that is accepted, you can work around the issue by updating your root Vitest config:pre-commit
tasks?
import { defineConfig } from 'vitest/config';
export default defineConfig({ clearScreen: process.argv.includes('--watch'),});