Vitest

1 min read


Hello Stackers, now we will get acquainted and practice unit testing using Vitest.

A Vite-native unit test framework. It’s fast!

Vitest

Quick Start

Vitest requires Vite >=v2.7.10 and Node >=v14

yarn add -D vitest

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 and plugins configuration will work out-of-the-box. If you want a different configuration during testing, you can:

  • Create vitest.config.ts, which will have the higher priority
  • Pass --config option to CLI, e.g. vitest --config ./path/to/vitest.config.ts
  • Use process.env.VITEST or mode property on defineConfig (will be set to test if not overridden) to conditionally apply different configuration in vite.config.ts

To configure vitest itself, add test property in your Vite config. You’ll also need to add a reference to Vitest types using a triple slash command at the top of your config file, if you are importing defineConfig from vite itself.

import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    // ...
  },
})

Command Line Interface#

In a project where Vitest is installed, you can use the vitest binary in your npm scripts, or run it directly with npx vitest. Here are the default npm scripts in a scaffolded Vitest project:

{
  "scripts": {
    "test": "vitest",
    "coverage": "vitest run --coverage"
  }
}

Okay, for the full documentation available on here … cyaaa …

Bima Sena

Leave a Reply

Your email address will not be published. Required fields are marked *