Questions tagged [vitest]

Vitest is a blazing-fast unit test framework powered by Vite.

501 questions
54
votes
8 answers

Vitest defineConfig, 'test' does not exist in type 'UserConfigExport'

Trying to setup vitest on an already existing vite (vue 3, typescript) project. My vite.config.ts looks like this: import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue'; export default defineConfig({ test: { globals:…
WillD
  • 5,170
  • 6
  • 27
  • 56
24
votes
3 answers

Vitest - @ src folder alias not resolved in test files

I have a vue3 project using Vite/Vitest, as recommanded in Vue.js documentation. Here is the structure of the project: src components // my Vue components here, potentially in sub-folders. For example: HelloWorld.vue router …
Eria
  • 2,653
  • 6
  • 29
  • 56
15
votes
1 answer

Jest to vitest migration error; equivalent of moduleNameMapper

I am trying to migrate from jest to vitest. at some point I get this error : Syntax Error: Invalid or unexpected token It seems one of the packages we have in project has this line that causes the issue: require("./lib/somefont.woff") I checked…
meisam
  • 465
  • 6
  • 18
14
votes
1 answer

How to clear/reset mocks in Vitest

I have a simple composable useRoles which I need to test import { computed } from "vue"; import { useStore } from "./store"; export default function useRoles() { const store = useStore(); const isLearner = computed(() =>…
n1md7
  • 2,854
  • 1
  • 12
  • 27
11
votes
6 answers

How to mock/stub vue-i18n?

I have started to replace Jest with Vitest for my unit test library in my Vue 3 App. I am trying to write unit test for a component that uses the vue-i18n library to translate text within it but when I try to mount this component in my test file, it…
Mac
  • 1,025
  • 3
  • 12
  • 22
10
votes
1 answer

VS Code showing eslint error but vitest is working. 'vi' is not defined

I have a tsconfig file like this { "include": ["tests/**/*.ts"], "exclude": [], "compilerOptions": { "composite": true, "lib": [], "skipLibCheck": true, "outDir": "lib", "types": ["vitest/globals"] } } as I have defined…
coure2011
  • 40,286
  • 83
  • 216
  • 349
10
votes
2 answers

'test' does not exist in type 'UserConfigExport', even with reference types

As described in this answer and in the docs. I've added the reference types for Vitest at the top of my Vite config file. /// Why am I still getting the TypeScript warning 'test' does not exist in type…
Zoford
  • 123
  • 8
10
votes
2 answers

How to keep tests outside from source directory in Vite projects?

Running npm init vue@latest with the following setup generates a Vitest spec file inside the src directory. I'm wondering why Cypress e2e tests have a seperate directory and Vitest unit tests are right next to the source code. Are there any…
user17298649
9
votes
2 answers

How to initialise Vuelidate 2.0.0 when testing a Vue component with Vitest?

In my Vue 2.7.14 app I'm using Vuelidate 2.0.0. I'm trying to migrate a test from Jest to Vitest, but the v$ object is not initialised correctly in the latter case. The component has a checkbox bound to formData.accepted validations () { …
Dónal
  • 185,044
  • 174
  • 569
  • 824
8
votes
2 answers

Vitest mock modules function in only one test and use the actual function in others

The following is an abstraction of my problem and thus does not make too much sense: Given I have a simple utility callMethodIf that's returning the return of another imported method (blackbox). ~~/utils/call-method-if.js: import { blackbox } from…
floriankapaun
  • 472
  • 1
  • 4
  • 19
7
votes
6 answers

Mock react-router-dom useParams hook in vitest

I just started to migrate from jest to vitest after migrating my app from cra to vite. I ran ainto an issue where I want to mock useParam hook of react-router-dom Original code: jest.mock('react-router-dom', () => ({ …
Aaliyah
  • 71
  • 1
  • 3
7
votes
2 answers

Vitest - How to exclude specific files and folders?

I am migrating my project from jest to vitest and I wanna exclude certain files and folders for both test and coverage, I am following the docs but "exclude" does not seem working, whenever I run some test, vitest throws an error that is coming from…
j.doe
  • 73
  • 1
  • 5
7
votes
2 answers

vitest crypto.randomUUID() is not a function

vite.config.ts import { sveltekit } from '@sveltejs/kit/vite'; const config = { plugins: [sveltekit()], test: { include: ['**/*.spec.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], environment: 'jsdom', globals: true, …
Lun
  • 861
  • 1
  • 10
  • 18
7
votes
1 answer

vuejs/vitest: How to test/mock composition functions

I’m kinda stuck with testing composition-functions. I’m using vitest with @vue/test-utils, but also found no good way to get it to work with jest. My issue is: How do I test composable functions, which use other composable functions? And how do I…
DotCoyote
  • 85
  • 2
  • 6
7
votes
2 answers

Is it possible to use different tsconfig file for vitest

Is there any way to use different tsconfig file for vitest? I have a large project using vue-cli. Before converting it to vite based I first want to use vitest instead of jest. The problem is that the tsconfig.json file is using 'target: es2015' and…
coure2011
  • 40,286
  • 83
  • 216
  • 349
1
2 3
33 34