How to setup Vitest for Node.js testing

May 25, 2022

4 min read

views

Requirements

  • Node.js installed with version v14 or higher
  • Basic Node.js app

Installation

Vitest is super simple to install. It can be installed via any node package manager such as npm, yarn or pnpm. We will install vitest as a devDependency.

npm install --save-dev vitest
# OR
yarn add -D vitest
# OR
pnpm add -D vitest
bash

Configuring the project

We will now add a simple command to our package.json. This will allow us to run npm run test in the command line.

{
  "name": "my-nodejs-app",
  "version": "0.0.0",
  "scripts": {
    "test": "vitest"
  }
}
json

Adding a test file

Once we've updated our configuration, we can start adding test files, for example maths.test.ts. We import test and expect from vitest:

maths.test.ts
import { expect, test } from 'vitest'
 
test('Math.sqrt()', () => {
  expect(Math.sqrt(4)).toBe(2)
  expect(Math.sqrt(144)).toBe(12)
  expect(Math.sqrt(2)).toBe(Math.SQRT2)
})
typescript

Testing the application

To run these tests, simply run the test command we created earlier:

npm run test
bash

This will show all passed and failed tests.

Tip: You can also view those tests in the browser! Simply create a new command shown below:

{
  "scripts": {
    "test": "vitest",
    "test:ui": "vitest --ui"
  }
}
json

Online Example

More from Vitest

You can use Vitest for many more project types, such as: React, Next.js, Vue, Svelte, etc. Read more about the awesomeness Vitest can do at their documentation

Personally, I use Vitest in many of my projects and I absolutely love it! It's simple to setup, fast and has a great community.