Vitest is a blazing-fast unit test framework powered by Vite.
Questions tagged [vitest]
501 questions
6
votes
1 answer
Fail to run Vitest test with an error: "Vitest caught 1 unhandled error during the test run"
I have a function which I try to test using Vitest.
This is my function (I test the exported one):
import os from 'node:os';
import { AI_COMMIT_IGNORED_FILES } from '../constants/ai-commit';
import { asyncExec } from './os';
/**
* The function…

Tal Rofe
- 457
- 12
- 46
6
votes
0 answers
Vitest config doesn't detect jsdom environment
I'm in a Vite/React/TypeScript application and I'm configuring my first test with Vitest.
When I run my Button test (yarn vitest), I get this error:
packages/frontend/src/components/Generic/Button/Button.test.tsx > Button > should render the…

tomakazub
- 237
- 1
- 2
- 8
6
votes
1 answer
nuxt3: how to test SSR page with form, useFetch() and pinia?
In my Nuxt3 project, I have a very basic login page which is SSR like the following:
pages/login.vue
6
votes
0 answers
Vitest throws error upon import statement of file
I'm using Vitest to test my JavaScript project that uses the Three.js graphics library. The generateRandomMoves function in cube.js doesn't depend on anything in index.js to run. The test appears to fail because cube.js imports index.js, which…

joembarron
- 141
- 7
6
votes
2 answers
Vitest not recognizing absolute import
I can't find what I'm doing wrong. I'm getting this error when I'm running vitest test.
It seems that import { Typography, icons } from 'ui-components'; not working for me.
src/tests/integration/layouts/SideBarLayout/UserProfile/UserProfile.test.tsx…

Peroman86
- 61
- 1
- 4
6
votes
4 answers
How to I access my .env variables from a vitest test?
I am trying to migrate my tests from jest to vitest. I have a test suite that uses the dotenv package to pull in my .env variables.
I have this in my test suite
beforeAll(async () => {
vi.clearAllMocks();
cleanUpMetadata();
…

Bebet
- 199
- 2
- 12
6
votes
1 answer
What is the intended test structure of vitest inside Vue projects?
I created a new Vue project via npm init vue@latest ( with vitest ). Inside ./src/components is a __test__ directory containing a component unit test.
Is it intended to create another __test__ directory for the ./src/views directory if you want to…
user19025657
6
votes
1 answer
Vue3- Using Vitest toHaveBeenCalled() method
I am running a vue3 application using the Composition API and the setup() hook.
I am using Vitest as unit-test framework. (v 0.6.1)
I have the following sample component :
// src/components/MyComponent.vue
counter : {{…

Deltantoine
- 61
- 1
- 2
6
votes
1 answer
How to debug vitest in Visual Studio code. Currently have "unbound" breakpoints
According to the Vitest documentation, this is supposed to allow debugging of Vitest in visual Studio code. So I have the following in launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
…

GeeWhizBang
- 699
- 4
- 27
5
votes
1 answer
vitest vi.spyOn does not work on side effects?
I would like to use `vi.spyOn` to monitor the call to an sideEffect function
in a module to make sure is being called by another function in a module.
I did this on jest without problems but it does not seem to work on vitest.
Here is a simplified…

micurs
- 543
- 4
- 8
5
votes
0 answers
How to mock a typescript class with vitest
I have a class A that imports class B from a different module and instantiates it. In my test for class A I'd like to stub/mock some of the methods of class B.
Some example code:
// Formatter.ts
export class Formatter {
format(msg: string):…

chiborg
- 26,978
- 14
- 97
- 115
5
votes
3 answers
How can I solve the issue of 'failed to resolve import in '@/...'" vitest?
This is the error I got. There is a problem with the file path I defined in the "vite.config.ts" file. Can you help me?
Error Log
Error Message:
FAIL tests/utils/ConvertFromDomainToCountryCode.test.ts […

mahmutoz
- 131
- 1
- 7
5
votes
1 answer
Vitest setup auto-import for Nuxt and pinia
I'm trying to setup vitest + @vue/test-utils but I can't test some components that use ref(Vue) or useCookie (Nuxt), I have the same problem with Pinia as well where I import globally 2 functions in the nuxt.config.js like this
[
'@pinia/nuxt',
…

Seba
- 617
- 1
- 8
- 33
5
votes
2 answers
how can i do a unit testing of axios.create mock with vitest?
i am learning unit testing in vue with typescript and i want to test this function
const getCLients =async (): Promise => {
const {data} = await clientsApi.get('/clients')
return data
}
But i got an error due to the…

alain_maza
- 71
- 4
5
votes
2 answers
How to write a unit test in vitest that expects an error
I want to mock an object method that throws an error but the test is always failing with these errors.
I can't post the image yet but please check the link. It shows the test failing.
throw new Error
Here's my code below:
workload.js
const workload…

Shu Pesmerga
- 63
- 1
- 5