网页 综合 更新时间 2026-09-28 09:25:03

Getting Started | Guide | Vitest

自动蜘蛛 vitest.dev 正文约 3128 字
Getting Started | Guide | Vitest
Writing Tests with AI
Visual Regression Testing
Profiling Test Performance
Improving Performance
Database Transaction per Test
Cancelling Long-Running Operations Gracefully
Waiting for Async Conditions
Type Narrowing in Tests
Custom Assertion Helpers
Watching Non-Imported Files
Extending Browser Locators
Schema-Driven Assertions
Auto-Cleanup with `using`
Conditional Mocking with `vi.when`
Per-File Isolation Settings
Parallel and Sequential Test Files
Running Only Affected Tests
Running Tests via API
Migrating to Vitest 5.0
Migrating from Mocha + Chai + Sinon
Vitest (pronounced as "veetest") is a next generation testing framework powered by Vite.
You can learn more about the rationale behind the project in the Why Vitest section.
You can try Vitest online on StackBlitz. It runs Vitest directly in the browser, and it is almost identical to the local setup but doesn't require installing anything on your machine.
Vitest requires Vite >=v6.4.0 and Node >=v22.12.0
It is recommended that you install a copy of vitest in your package.json, using one of the methods listed above. However, if you would prefer to run vitest directly, you can use npx vitest (the npx tool comes with npm and Node.js).
The npx tool will execute the specified command. By default, npx will first check if the command exists in the local project's binaries. If it is not found there, npx will look in the system's $PATH and execute it if found. If the command is not found in either location, npx will install it in a temporary location prior to execution.
Vitest and third party integrations can use .vitest directory to store generated artifacts. It's recommended to add this in your .gitignore.
As an example, we will write a simple test that verifies the output of a function that adds two numbers.
By default, tests must contain .test. or .spec. in their file name.
Next, in order to execute the test, add the following section to your package.json:
Finally, run npm run test, yarn test or pnpm test, depending on your package manager, and Vitest will print this message:
If you are using Bun as your package manager, make sure to use bun run test command instead of bun test, otherwise Bun will run its own test runner.
Your first test is passing! Continue to Writing Tests to learn about organizing tests, reading test output, and the core testing patterns you'll use every day.
To run tests once without watching for file changes, use vitest run. You can also pass additional flags like --reporter or --coverage. For a full list of CLI options, run npx vitest --help or see the CLI guide.
Vitest reads your vite.config.* by default, so your existing Vite plugins and configuration work out-of-the-box. You can also create a dedicated vitest.config.* for test-specific settings. See the Config Reference for details.
We also provided an official extension for Visual Studio Code to enhance your testing experience with Vitest.
Install from VS Code Marketplace
Learn more about IDE Integrations
If you have questions or need help, reach out to the community at Discord and GitHub Discussions.
© 2026 VoidZero Inc. and Vitest contributors.
访问原文 返回搜索 https://vitest.dev/guide/
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。