Skip to content

配置索引

如果我们正在使用 Vite 并且拥有一个 vite.config 文件,Vitest 会读取它来匹配我们的 Vite 应用的插件和设置。如果我们想要为测试配置不同的设置,或者我们的并不特别依赖于 Vite,我们我们可以选择:

  • 创建 vitest.config.ts,它将具有更高的优先级,并且会覆盖 vite.config.ts 中的配置(Vitest 支持所有传统的 JS 和 TS 文件扩展名,但不支持 json) - 这意味着我们在 vite.config 中的所有选项将被忽略
  • 向 CLI 传递 --config 选项,例如 vitest --config ./path/to/vitest.config.ts
  • 使用 process.env.VITEST 或在 defineConfig 上的 mode 属性(如果没有用 --mode 覆盖,默认设置为 test/benchmark)来在 vite.config.ts 中有条件地应用不同的配置。请注意,像任何其他环境变量一样,VITEST 也会在测试中的 import.meta.env 上暴露出来。

要配置 Vitest 本身,请在我们的 Vite 配置中添加 test 属性。如果我们是从 vite 本身导入 defineConfig,我们还需要在配置文件顶部使用 三斜杠指令 添加对 Vitest 类型引用。

If you are not using vite, add defineConfig imported from vitest/config to your config file:

vitest.config.js
js
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    // ... 在此指定选项。
  },
})

If you have a vite config already, you can add /// <reference types="vitest/config" /> to include the test types:

vite.config.js
js
/// <reference types="vitest/config" />
import { defineConfig } from 'vite'

export default defineConfig({
  test: {
    // ... 在此指定选项。
  },
})

You can retrieve Vitest's default options to expand them if needed:

vitest.config.js
js
import { configDefaults, defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    exclude: [...configDefaults.exclude, 'packages/template/*'],
  },
})

当使用单独的 vitest.config.js 时,我们还可以根据需要从另一个配置文件扩展 Vite 的选项:

vitest.config.js
js
import { defineConfig, mergeConfig } from 'vitest/config'
import viteConfig from './vite.config'

export default mergeConfig(viteConfig, defineConfig({
  test: {
    exclude: ['packages/template/*'],
  },
}))

如果我们的 Vite 配置定义为一个函数,我们可以像这样定义配置:

vitest.config.js
js
import { defineConfig, mergeConfig } from 'vitest/config'
import viteConfig from './vite.config'

export default defineConfig(configEnv => mergeConfig(
  viteConfig(configEnv),
  defineConfig({
    test: {
      exclude: ['packages/template/*'],
    },
  })
))

由于 Vitest 使用 Vite 的配置,我们也可以使用 Vite 中的任何配置选项。例如,使用 define 来定义全局变量,或者使用 resolve.alias 来定义别名——这些选项应该在顶级定义,而不是在 test 属性内部。

Configuration options that are not supported inside a project config have

icon next to them. This means they can only be set in the root Vitest config.

Released under the MIT License.