Skip to content

@onerepo/plugin-vitest

Install via npm
npm 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:

  1. 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({});
  2. Create a Workspace config

    In the vitest.workspace.ts file, we need to duplicate the workspace globs from our original package.json or pnpm-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',
    ]);
  3. Create each Workspace configs

    In each Workspace that has tests, create a new Vitest config file, ensuring to use defineProject (not defineConfig):

    apps/my-app/vitest.config.ts
    import { defineProject } from 'vitest/config';
    export default defineProject({});
vitest(opts): PluginObject

Include the vitest plugin in your oneRepo plugin setup:

onerepo.config.ts
import { vitest } from '@onerepo/plugin-vitest';
export default {
plugins: [vitest()],
};
ParameterType
optsOptions
type Options: {
config: string;
name: string | string[];
};

Options for configuring the Vitest plugin for oneRepo.

onerepo.config.js
export default {
plugins: [
vitest({
name: ['test'],
}),
],
};
optional config: string;

Specify the main Jest configuration file, if different from <repo>/vitest.config.js. This can be relative to the repository root.

onerepo.config.js
export default {
plugins: [
vitest({
config: 'configs/vitest.config.js'
}),
],
});
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.

onerepo.config.js
export default {
plugins: [
vitest({
name: ['test', 'vitest']
}),
],
});
./onerepo.config.ts
import type { Config } from 'onerepo';
export default {
tasks: {
'pre-commit': {
serial: ['$0 vitest'],
},
'pre-merge': {
serial: ['$0 vitest -a'],
},
},
} satisfies Config;

Aliases: one test

Run unit tests using Vitest

Terminal window
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 --).

OptionTypeDescription
--affectedbooleanSelect all affected Workspaces. If no other inputs are chosen, this will default to true.
--all, -abooleanRun across all Workspaces
--inspectbooleanBreak for the the Node inspector to debug tests.
--stagedbooleanUse 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.
--watchbooleanShortcut for vitest --watch mode.
--workspaces, -warrayList of Workspace names to run against
Advanced options
OptionTypeDescription
--configstring, default: "./vitest.config.ts"Path to the vitest.config file, relative to the repo root.
--from-refstringGit ref to start looking for affected files or Workspaces
--show-advancedbooleanPair with --help to show advanced options.
--through-refstringGit ref to start looking for affected files or Workspaces

Run only tests related to modified files.

Terminal window
one vitest ,test

Run vitest in —watch mode.

Terminal window
one vitest ,test --watch

Run vitest in watch mode with a particular file.

Terminal window
one vitest ,test --watch -- path/to/test.ts

Run all tests in a given Workspace.

Terminal window
one vitest ,test -w <workspaces...>

How can I stop Vitest from clearing the output when running in pre-commit tasks? 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:

"./vitest.config.ts"n
import { defineConfig } from 'vitest/config';
export default defineConfig({
clearScreen: process.argv.includes('--watch'),
});