Skip to content

源码内联测试

Vitest 还提供了一种方式,可以运行与你的代码实现放在一起的测试,就像是 Rust 语言的模块测试一样

这允许测试与实现共享相同的闭包,并且能够在不导出的情况下针对私有状态进行测试。同时,它也使开发更加接近反馈循环。

指引

首先,在 if (import.meta.vitest) 代码块内写一些测试代码并放在文件的末尾,例如:

ts
// src/index.ts

// 函数实现
export function add(...args: number[]) {
  return args.reduce((a, b) => a + b, 0)
}

// 源码内的测试套件
if (import.meta.vitest) {
  const { it, expect } = import.meta.vitest
  it('add', () => {
    expect(add()).toBe(0)
    expect(add(1)).toBe(1)
    expect(add(1, 2, 3)).toBe(6)
  })
}

更新 Vitest 配置文件内的 includeSource 以获取到 src/ 下的文件:

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

export default defineConfig({
  test: {
    includeSource: ['src/**/*.{js,ts}'], 
  },
})

然后你就可以开始执行测试了!

bash
$ npx vitest

生产环境构建

对于生产环境的构建,你需要设置配置文件内的 define 选项,让打包器清除无用的代码。例如,在 Vite 中

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

export default defineConfig({
  test: {
    includeSource: ['src/**/*.{js,ts}'],
  },
  define: {

    'import.meta.vitest': 'undefined', 
  }, 
})

其他的打包器

unbuild
ts
// build.config.ts
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
  replace: {

    'import.meta.vitest': 'undefined', 
  }, 
  // other options
})

了解更多:unbuild

Rollup
ts
// rollup.config.js
import replace from '@rollup/plugin-replace'

export default {
  plugins: [
    replace({

      'import.meta.vitest': 'undefined', 
    }), 
  ],
  // other options
}

了解更多:Rollup

TypeScript

要获得对 import.meta.vitest 的 TypeScript 支持,添加 vitest/importMetatsconfig.json:

json
// tsconfig.json
{
  "compilerOptions": {
    "types": [
      "vitest/importMeta"
    ]
  }
}

完整的示例请参考 test/import-meta

说明

此功能可用于:

  • 小范围的功能或 utils 工具的单元测试
  • 原型设计
  • 内联断言

对于更复杂的测试,比如组件测试或 E2E 测试,建议使用单独的测试文件取而代之

Released under the MIT License.