| [ Web Proxy ] |
| Viewing: https://cn.vitest.dev/guide/features | [Back] [Original] |
Vite
$ vitestVitest HMR Vite !
vitest CI process.env.CI (run mode) vitest watch vitest run
--standalone VitestVitest
ES Module / TypeScript / JSX support / PostCSS
Vitest node:child_process --pool=threads node:worker_threads / fileParallelism
Vitest --no-isolate CLI
Vitest
.concurrent
import { describe, it } from 'vitest'
//
describe('suite', () => {
it('serial test', async () => {
/* ... */
})
it.concurrent('concurrent test 1', async ({ expect }) => {
/* ... */
})
it.concurrent('concurrent test 2', async ({ expect }) => {
/* ... */
})
}) .concurrent
import { describe, it } from 'vitest'
//
describe.concurrent('suite', () => {
it('concurrent test 1', async ({ expect }) => {
/* ... */
})
it('concurrent test 2', async ({ expect }) => {
/* ... */
})
it.concurrent('concurrent test 3', async ({ expect }) => {
/* ... */
})
}) .skip.only .todo
import { expect, it } from 'vitest'
it('renders correctly', () => {
const result = render()
expect(result).toMatchSnapshot()
})expect Chai Jest expect APIs
test.globals true
Vitest vi Jest API
import { expect, vi } from 'vitest'
const fn = vi.fn()
fn('hello', 1)
expect(vi.isMockFunction(fn)).toBe(true)
expect(fn.mock.calls[0]).toEqual(['hello', 1])
fn.mockImplementation((arg: string) => arg)
fn('world', 2)
expect(fn.mock.results[1].value).toBe('world')Vitest happy-dom jsdom DOM APIVitest
$ npm i -D happy-dom$ npm i -D jsdom environment
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
environment: 'happy-dom', // 'jsdom' 'node'
},
}){
"scripts": {
"test": "vitest",
"coverage": "vitest run --coverage"
}
} test.coverage
// vite.config.ts
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
coverage: {
reporter: ['text', 'json', 'html'],
},
},
})Vitest Rust .
//
export function add(...args: number[]): 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)
})
}import { bench, describe } from 'vitest'
describe('sort', () => {
bench('normal', () => {
const x = [1, 5, 4, 2, 3]
x.sort((a, b) => {
return a - b
})
})
bench('reverse', () => {
const x = [1, 5, 4, 2, 3]
x.reverse().sort((a, b) => {
return a - b
})
})
}) Vitest expect-type API
import { assertType, expectTypeOf, test } from 'vitest'
import { mount } from './mount.js'
test('my types work properly', () => {
expectTypeOf(mount).toBeFunction()
expectTypeOf(mount).parameter(0).toExtend<{ name: string }>()
// @ts-expect-error
assertType(mount({ name: 42 }))
}) --shard --reporter=blob --merge-reports CI
vitest --shard=1/2 --reporter=blob --coverage
vitest --shard=2/2 --reporter=blob --coverage
vitest --merge-reports --reporter=junit --coverageVitest .env VITE_ Vite .env vite loadEnv
import { loadEnv } from 'vite'
import { defineConfig } from 'vitest/config'
export default defineConfig(({ mode }) => ({
test: {
// mode env.{mode}
env: loadEnv(mode, process.cwd(), ''),
},
}))Vitest (Promise) Node.js
Vitest
// Node.js
process.on('unhandledRejection', () => {
//
})
process.on('uncaughtException', () => {
//
})//
window.addEventListener('error', () => {
//
})
window.addEventListener('unhandledrejection', () => {
//
}) dangerouslyIgnoreUnhandledErrors Vitest
test('my function throws uncaught error', async ({ onTestFinished }) => {
const unhandledRejectionListener = vi.fn()
process.on('unhandledRejection', unhandledRejectionListener)
onTestFinished(() => {
process.off('unhandledRejection', unhandledRejectionListener)
})
callMyFunctionThatRejectsError()
await expect.poll(unhandledRejectionListener).toHaveBeenCalled()
}):
2026 VoidZero Inc. Vitest
| Web Proxy Viewer | New URL | Original Page |