[ Web Proxy ]
URL:
Viewing: https://cn.vitest.dev/guide/features [Back]  [Original]

| | Vitest
Skip to content
Menu
Return to top
Sidebar Navigation

On this page

    Vitest Vitest

    Vite

    Vitest

    (watch mode)

    bash
    $ vitest

    Vitest HMR Vite !

    vitest CI process.env.CI (run mode) vitest watch vitest run

    --standalone VitestVitest

    Web

    ES Module / TypeScript / JSX support / PostCSS

    Vitest node:child_process --pool=threads node:worker_threads / fileParallelism

    Vitest --no-isolate CLI

    Vitest

    .concurrent

    ts
    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

    ts
    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

    API

    JavaScript expect

    Jest

    ts
    import { expect, it } from 'vitest'
    
    it('renders correctly', () => {
      const result = render()
      expect(result).toMatchSnapshot()
    })

    Chai Jest expect

    Chai Jest expect APIs

    test.globals true

    (Mocking)

    Vitest vi Jest API

    ts
    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

    happy-domjsdom
    bash
    $ npm i -D happy-dom
    bash
    $ npm i -D jsdom

    environment

    vitest.config.ts
    ts
    import { defineConfig } from 'vitest/config'
    
    export default defineConfig({
      test: {
        environment: 'happy-dom', //  'jsdom'  'node'
      },
    })

    Vitest v8 istanbul

    package.json
    json
    {
      "scripts": {
        "test": "vitest",
        "coverage": "vitest run --coverage"
      }
    }

    test.coverage

    ts
    // vite.config.ts
    import { defineConfig } from 'vitest/config'
    
    export default defineConfig({
      test: {
        coverage: {
          reporter: ['text', 'json', 'html'],
        },
      },
    })

    Vitest Rust .

    src/index.ts
    ts
    // 
    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)
      })
    }

    bench Tinybench

    sort.bench.ts
    ts
    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
        })
      })
    })
    Benchmark report [Benchmark report]Benchmark report [Benchmark report]

    Vitest expect-type API

    types.test-d.ts
    ts
    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

    bash
    vitest --shard=1/2 --reporter=blob --coverage
    vitest --shard=2/2 --reporter=blob --coverage
    vitest --merge-reports --reporter=junit --coverage

    |

    Vitest .env VITE_ Vite .env vite loadEnv

    vitest.config.ts
    ts
    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

    setup.node.jssetup.browser.js
    ts
    //  Node.js
    process.on('unhandledRejection', () => {
      // 
    })
    
    process.on('uncaughtException', () => {
      // 
    })
    ts
    // 
    window.addEventListener('error', () => {
      // 
    })
    
    window.addEventListener('unhandledrejection', () => {
      // 
    })

    dangerouslyIgnoreUnhandledErrors Vitest

    ts
    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()
    })
    Pager

    Web Proxy Viewer  |  New URL  |  Original Page