From eaa623ca3ca5acc7179f55e343a9279071c5c7f0 Mon Sep 17 00:00:00 2001 From: Jannchie Date: Sun, 30 Jul 2023 02:29:59 +0900 Subject: [PATCH 1/3] doc: add writing tests section in getting started --- docs/guide/index.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/docs/guide/index.md b/docs/guide/index.md index c466396b3c64..d578af209b2f 100644 --- a/docs/guide/index.md +++ b/docs/guide/index.md @@ -38,6 +38,48 @@ It is recommended that you install a copy of `vitest` in your `package.json`, us 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. +## Writing Tests + +As an example, we will write a simple test that verifies the output of a function that adds two numbers. + +``` js +// sum.js +export function sum (a, b) { + return a + b +} + +// sum.test.js +import { sum } from './sum' +import { expect, test } from 'vitest' +test('adds 1 + 2 to equal 3', () => { + expect(sum(1, 2)).toBe(3) +}) +``` + +Next, in order to execute the test, add the following section to your `package.json`: + +```json +{ + "scripts": { + "test": "vitest" + } +} +``` + +Finally, run `yarn test`, `npm test` or `pnpm test` and Vitest will print this message: + +```log +✓ sum.test.js (1) + ✓ adds 1 + 2 to equal 3 + +Test Files 1 passed (1) + Tests 1 passed (1) + Start at 02:15:44 + Duration 311ms (transform 23ms, setup 0ms, collect 16ms, tests 2ms, environment 0ms, prepare 106ms) +``` + +Learn more about the usage of Vitest, see the [API](https://vitest.dev/api/) section. + ## Configuring Vitest 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: From c28acdabbcdb5bc2de2d42c09f354a0f73a26750 Mon Sep 17 00:00:00 2001 From: Jianqi Pan Date: Sun, 30 Jul 2023 02:46:19 +0900 Subject: [PATCH 2/3] Update docs/guide/index.md Co-authored-by: Vladimir --- docs/guide/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide/index.md b/docs/guide/index.md index d578af209b2f..cece16cee56d 100644 --- a/docs/guide/index.md +++ b/docs/guide/index.md @@ -66,7 +66,7 @@ Next, in order to execute the test, add the following section to your `package.j } ``` -Finally, run `yarn test`, `npm test` or `pnpm test` and Vitest will print this message: +Finally, run `npm run test`, `yarn test`, or `pnpm test`, depending on your package manager, and Vitest will print this message: ```log ✓ sum.test.js (1) From a75cac2b65ec08c671a979267d0313e5b4f56e15 Mon Sep 17 00:00:00 2001 From: Jannchie Date: Sun, 30 Jul 2023 13:47:30 +0900 Subject: [PATCH 3/3] fix: lint --- docs/guide/index.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/guide/index.md b/docs/guide/index.md index d578af209b2f..df9b14e2220a 100644 --- a/docs/guide/index.md +++ b/docs/guide/index.md @@ -44,13 +44,16 @@ As an example, we will write a simple test that verifies the output of a functio ``` js // sum.js -export function sum (a, b) { +export function sum(a, b) { return a + b } +``` +``` js // sum.test.js -import { sum } from './sum' import { expect, test } from 'vitest' +import { sum } from './sum' + test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3) })