公司網(wǎng)站開(kāi)發(fā)流程百度首頁(yè)純凈版
項(xiàng)目目錄
runner
-
Type: VitestRunnerConstructor
-
Default:
-
node, 當(dāng)運(yùn)行test的時(shí)候
-
benchmark,當(dāng)運(yùn)行bench測(cè)試的時(shí)候
-
-
功能
-
自定義測(cè)試運(yùn)行程序的路徑。
-
-
要求
-
應(yīng)與自定義庫(kù)運(yùn)行程序一起使用。
-
如果您只是運(yùn)行測(cè)試,則可能不需要這個(gè)。它主要由library作者使用
-
benchmark
-
Type: { include?, exclude?, ... }
-
值
-
運(yùn)行
vitest bench
時(shí)使用的選項(xiàng)
-
benchmark.include 基準(zhǔn)測(cè)
-
Type: string[]
-
默認(rèn)值:
['**/*.{bench,benchmark}.?(c|m)[jt]s?(x)']
-
值:包括基準(zhǔn)測(cè)試文件的 glob
-
配置代碼1
/// <reference types="vitest" /> import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue'// https://vitejs.dev/config/ export default defineConfig({plugins: [vue()],test: {benchmark: {include: ["./scripts/*.{bench,benchmark}.?(c|m)[jt]s?(x)"]// include: ["./src/tests/*.{bench,benchmark}.?(c|m)[jt]s?(x)"]}} })
? ? ?
-
配置代碼2
/// <reference types="vitest" /> import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue'// https://vitejs.dev/config/ export default defineConfig({plugins: [vue()],test: {benchmark: {// include: ["./scripts/*.{bench,benchmark}.?(c|m)[jt]s?(x)"]include: ["./src/tests/*.{bench,benchmark}.?(c|m)[jt]s?(x)"]}} })
? ? ?
-
配置代碼3
/// <reference types="vitest" /> import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue'// https://vitejs.dev/config/ export default defineConfig({plugins: [vue()],test: {benchmark: {// include: ["./scripts/*.{bench,benchmark}.?(c|m)[jt]s?(x)"]// include: ["./src/tests/*.{bench,benchmark}.?(c|m)[jt]s?(x)"]}} })
? ?
-
測(cè)試結(jié)果1
-
-
測(cè)試結(jié)果2
-
-
測(cè)試結(jié)果3
-
benchmark.exclude
-
Type: string[]
-
Default: ['node_modules', 'dist', '.idea', '.git', '.cache']
-
值:排除基準(zhǔn)測(cè)試文件的 glob
-
代碼配置
/// <reference types="vitest" /> import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue'// https://vitejs.dev/config/ export default defineConfig({plugins: [vue()],test: {benchmark: {include: ["./scripts/*.{bench,benchmark}.?(c|m)[jt]s?(x)"],exclude: ["./src/tests/*.{bench,benchmark}.?(c|m)[jt]s?(x)"]}} })
-
測(cè)試結(jié)果
-
benchmark.includeSource
-
Type: string[]
-
Default: []
-
值:包括源內(nèi)基準(zhǔn)測(cè)試文件的 glob
-
功能
-
定義后,Vitest 將運(yùn)行所有包含
import.meta.vitest
的匹配文件
-
-
配置代碼
/// <reference types="vitest" /> import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue'// https://vitejs.dev/config/ export default defineConfig({plugins: [vue()],test: {benchmark: {includeSource: ["./scripts/*.{bench,benchmark}.?(c|m)[jt]s?(x)"],}} })
-
測(cè)試結(jié)果
-
benchmark.reporters
-
Type: Arrayable<BenchmarkBuiltinReporters | Reporter>
-
Default: 'default'
-
值:
-
用于輸出的自定義報(bào)告器。
-
可以包含一個(gè)或多個(gè)
-
內(nèi)置報(bào)告名稱
-
報(bào)告器實(shí)例
-
自定義報(bào)告器的路徑。
-
-
-
代碼配置
/// <reference types="vitest" /> import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue'// https://vitejs.dev/config/ export default defineConfig({plugins: [vue()],test: {benchmark: {reporters: 'default'}} })
-
測(cè)試結(jié)果
-
benchmark.outputFile
-
Type: string | Record<string, string>
-
功能:
-
當(dāng)指定了
--reporter=json
選項(xiàng)時(shí),將基準(zhǔn)測(cè)試結(jié)果寫(xiě)入文件。 -
通過(guò)提供對(duì)象而不是字符串,您可以在使用多個(gè)報(bào)告器時(shí)定義單獨(dú)的輸出。
-
-
使用
-
若要通過(guò) CLI 命令提供對(duì)象,請(qǐng)使用以下語(yǔ)法:
-
--outputFile.json=./path --outputFile.junit=./other-path
。
-
-
-
測(cè)試結(jié)果
-
alias
-
類型:
Record<string, string> | Array<{ find: string | RegExp, replacement: string, customResolver?: ResolverFunction | ResolverObject }>
-
功能:
-
在內(nèi)部測(cè)試中運(yùn)行時(shí)定義自定義別名。
-
-
注意:
-
它們將與
resolve.alias
中的別名合并。
-
globals
-
Type: boolean默認(rèn)情況下,
vitest
不提供全局 API 以便明確 -
Default: false
-
即默認(rèn)情況下,
vitest
不提供全局 API 以便明確
-
-
CLI: --globals, --globals=false
-
功能:
-
如果您想全局使用 API,可將
--globals
選項(xiàng)傳遞給 CLI 或在配置中添加globals: true
-
-
注意
-
要讓 TypeScript 使用全局 API,
-
請(qǐng)將
vitest/globals
添加到tsconfig.json
中的types
字段
// tsconfig.json {"compilerOptions": {"types": ["vitest/globals"]} }
-
-
如果您已經(jīng)在項(xiàng)目中使用
unplugin-auto-import
,您也可以直接使用它來(lái)自動(dòng)導(dǎo)入這些API// vitest.config.ts import { defineConfig } from 'vitest/config' import AutoImport from 'unplugin-auto-import/vite'export default defineConfig({plugins: [AutoImport({imports: ['vitest'],dts: true, // generate TypeScript declaration}),], })
-
-
代碼(不需要再引入API了)
//配置文件 /// <reference types="vitest" /> import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue'// https://vitejs.dev/config/ export default defineConfig({plugins: [vue()],test: {globals: true} })//test.js // import { bench } from 'vitest'//可以不再引入了bench('normal sorting', () => {const x = [1, 5, 4, 2, 3]x.sort((a, b) => {return a - b}) }, { time: 1000 })
-
測(cè)試結(jié)果
-
environment
-
Type: 'node' | 'jsdom' | 'happy-dom' | 'edge-runtime' | string
-
Default: 'node'
-
Vitest 中的默認(rèn)環(huán)境是 Node.js 環(huán)境。
-
-
CLI: --environment=<env>
-
值:將用于測(cè)試的環(huán)境
-
注意:
-
如果您正在構(gòu)建 Web 應(yīng)用程序,則可以通過(guò)
jsdom
或happy-dom
使用類似瀏覽器的環(huán)境。 -
如果您正在構(gòu)建邊緣功能,則可以使用
edge-runtime
環(huán)境 -
Vitest 還通過(guò)
vitest/environments
條目公開(kāi)builtinEnvironments
,以防您只想擴(kuò)展它
-
-
功能
-
您可以指定用于該文件中所有測(cè)試的另一個(gè)環(huán)境
-
-
使用
-
通過(guò)在文件頂部添加
@vitest-environment
文檔塊或注釋,-
Docblock style: 文檔塊樣式:
-
/// <reference types="vitest" /> import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue'// https://vitejs.dev/config/ export default defineConfig({plugins: [vue()],test: {environment: 'jsdom',globals: true} })
-
Comment style: 評(píng)論風(fēng)格:
// @vitest-environment happy-domtest('use happy-dom in this test file', () => {const element = document.createElement('div')expect(element).not.toBeNull() })
-
為了與 Jest 兼容,還有一個(gè)
@jest-environment
/*** @jest-environment jsdom*/test('use jsdom in this test file', () => {const element = document.createElement('div')expect(element).not.toBeNull() })
-
-
-
不同環(huán)境之間的測(cè)試順序
-
如果使用
--threads=false
標(biāo)志運(yùn)行 Vitest,您的測(cè)試將按以下順序運(yùn)行:-
node
、jsdom
、happy-dom
、edge-runtime
、custom environments
。-
這意味著,具有相同環(huán)境的每個(gè)測(cè)試都分組在一起,但仍然按順序運(yùn)行。
-
-
-
-
自定義環(huán)境(0.23.0開(kāi)始)
-
當(dāng)使用非內(nèi)置環(huán)境時(shí)
-
Vitest將嘗試加載包Vitest -environment-${name}。
-
該包應(yīng)該導(dǎo)出一個(gè)形狀為Environment的對(duì)象:
-
import type { Environment } from 'vitest'export default <Environment>{name: 'custom',transformMode: 'ssr',setup() {// custom setupreturn {teardown() {// called after all tests with this env have been run}}} }
-
-
-
-
配置代碼
/// <reference types="vitest" /> import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue'// https://vitejs.dev/config/ export default defineConfig({plugins: [vue()],test: {environment: 'jsdom',globals: true} })
-
測(cè)試結(jié)果
-
environmentOptions
-
Type: Record<'jsdom' | string, unknown>
-
Default: {}
-
功能:
-
這些選項(xiàng)將傳遞到當(dāng)前
environment
的setup
方法
-
-
注意:
-
默認(rèn)情況下,如果您使用 JSDOM 作為測(cè)試環(huán)境,則只能配置 JSDOM 選項(xiàng)。
-
environmentMatchGlobs
-
Type: string, EnvironmentName
-
Default: []
-
功能:
-
基于 glob 自動(dòng)分配環(huán)境。將使用第一個(gè)匹配。
-
-
代碼配置
import { defineConfig } from 'vitest/config'export default defineConfig({test: {environmentMatchGlobs: [// 在tests/dom中的所有測(cè)試都將在jsdom中運(yùn)行['tests/dom/**', 'jsdom'],//所有測(cè)試以.edge.test.ts結(jié)尾的。將在邊運(yùn)行時(shí)運(yùn)行['**\/*.edge.test.ts', 'edge-runtime'],// ...]} })
poolMatchGlobs
-
Type: string, 'threads' | 'forks' | 'vmThreads' | 'typescript'
-
Default: []
-
Version: Since Vitest 0.29.4
-
功能:
-
根據(jù) glob 自動(dòng)分配將在其中運(yùn)行測(cè)試的池
-
-
注意:
-
將使用第一個(gè)匹配的
-
-
代碼配置
import { defineConfig } from 'vitest/config'export default defineConfig({test: {poolMatchGlobs: [// all tests in "worker-specific" directory will run inside a worker as if you enabled `--threads` for them,//"worker-specific"目錄下的所有測(cè)試都將在worker中運(yùn)行,就像你為它們啟用了'——threads '一樣。['**/tests/worker-specific/**', 'threads'],// run all tests in "browser" directory in an actual browser//在實(shí)際的瀏覽器中運(yùn)行"browser"目錄下的所有測(cè)試['**/tests/browser/**', 'browser'],// all other tests will run based on "browser.enabled" and "threads" options, if you didn't specify other globs//所有其他測(cè)試將基于“browser.Enabled "和"threads"選項(xiàng)運(yùn)行,如果你沒(méi)有指定其他globs的話// ...]} })
update
-
Type: boolean
-
Default: false
-
CLI:
-u
、--update
、--update=false
-
功能:
-
更新快照文件。這將更新所有更改的快照并刪除過(guò)時(shí)的快照。
-
watch
-
Type: boolean
-
Default: true
-
CLI: -w, --watch, --watch=false
-
功能:
-
啟用觀看模式
-
root
-
Type: string
-
CLI: -r <path>, --root=<path>
-
功能
-
設(shè)置項(xiàng)目根目錄
-
reporters
-
Type: Reporter | Reporter[]
-
Default: 'default'
-
CLI: --reporter=<name>, --reporter=<name1> --reporter=<name2>
-
值:
-
用于輸出的自定義記者。
-
報(bào)告器可以是
-
報(bào)告器實(shí)例
-
用于選擇內(nèi)置報(bào)告器的字符串
-
自定義實(shí)現(xiàn)的路徑(例如
'./path/to/reporter.ts'
、'@scope/reporter'
-
-
outputFile
-
Type: string | Record<string, string>
-
CLI: --outputFile=<path>, --outputFile.json=./path
-
功能:
-
將測(cè)試結(jié)果寫(xiě)入文件。(當(dāng)還指定
--reporter=json
、--reporter=html
或--reporter=junit
選項(xiàng)時(shí)) -
使用多個(gè)報(bào)告器時(shí)定義單獨(dú)的輸出(通過(guò)提供對(duì)象而不是字符串)
-