[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

doc: add writing tests section in getting started (#3843)

Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>

authored by

Jianqi Pan
Vladimir
and committed by
GitHub
(Jul 30, 2023, 10:47 AM +0200) 2e5847ec d4f1e620

+45
+45
docs/guide/index.md
··· 38 38 39 39 The `npx` command will execute the command either from a local `node_modules/.bin` installing any packages needed in order for the command to run. By default, npx will check whether command exists in $PATH, or in the local project binaries, and execute that. If command is not found, it will be installed prior to execution. 40 40 41 + ## Writing Tests 42 + 43 + As an example, we will write a simple test that verifies the output of a function that adds two numbers. 44 + 45 + ``` js 46 + // sum.js 47 + export function sum(a, b) { 48 + return a + b 49 + } 50 + ``` 51 + 52 + ``` js 53 + // sum.test.js 54 + import { expect, test } from 'vitest' 55 + import { sum } from './sum' 56 + 57 + test('adds 1 + 2 to equal 3', () => { 58 + expect(sum(1, 2)).toBe(3) 59 + }) 60 + ``` 61 + 62 + Next, in order to execute the test, add the following section to your `package.json`: 63 + 64 + ```json 65 + { 66 + "scripts": { 67 + "test": "vitest" 68 + } 69 + } 70 + ``` 71 + 72 + Finally, run `npm run test`, `yarn test`, or `pnpm test`, depending on your package manager, and Vitest will print this message: 73 + 74 + ```log 75 + ✓ sum.test.js (1) 76 + ✓ adds 1 + 2 to equal 3 77 + 78 + Test Files 1 passed (1) 79 + Tests 1 passed (1) 80 + Start at 02:15:44 81 + Duration 311ms (transform 23ms, setup 0ms, collect 16ms, tests 2ms, environment 0ms, prepare 106ms) 82 + ``` 83 + 84 + Learn more about the usage of Vitest, see the [API](https://vitest.dev/api/) section. 85 + 41 86 ## Configuring Vitest 42 87 43 88 One of the main advantages of Vitest is its unified configuration with Vite. If present, `vitest` will read your root `vite.config.ts` to match with the plugins and setup as your Vite app. For example, your Vite [resolve.alias](https://vitejs.dev/config/shared-options.html#resolve-alias) and [plugins](https://vitejs.dev/guide/using-plugins.html) configuration will work out-of-the-box. If you want a different configuration during testing, you can: