Writing Unit Tests with Vitest
Learn how to write unit tests using Vitest, a lightweight and fast testing framework
All versions of Nextbase Starter Kit come with Vitest installed. Vitest is a lightweight and fast testing framework that allows you to write unit tests for your application.
Configuration
Vitest is configured in the vitest.config.ts
file.
As you can see, the configuration file is quite simple. You can customize it according to your needs.
Running tests
Vitest runs in NODE_ENV=test
mode. It also relies on .env.test
for environment variables.
Running Vitest
Writing tests
All the test cases are written in the tests
folder.
Helper Function Test case example
This is an example of a test case for a helper function.
Using Vitest with GitHub Actions
You can easily integrate Vitest with GitHub Actions to run your tests automatically on every push or pull request. Here is a basic example of a GitHub Actions workflow:
Most SaaS solo developers or small teams write integration tests more, hence we only added Vitest setup and a dummy test case, but you can update the test suite as needed.