Vitest is a blazing-fast unit test framework powered by Vite.
Questions tagged [vitest]
501 questions
5
votes
1 answer
Vue-Router Mock With Vue-Test-Utils & Vitest
I'm trying to understand the logic of mocking the Vue-Router with Vitest.
For this, I tried to set up and mock my test environment on a very simple project. When I tried to proceed according to the official documentation of Vue-Test-Utils, I always…

Mocha_
- 143
- 1
- 9
5
votes
1 answer
How to set/mock an env variable in vitest (`process.env.NODE_ENV = 'anything'` takes effect only in test file)?
I have a method, in a class, that only executes its action when NODE_ENV === 'test'.
Here is the test that I set the env to anything to test the failing scenario:
it('returns Left on clearDatabase when not in test environment', async () => {
const…

leal32b
- 334
- 2
- 9
5
votes
1 answer
How to debug Vitest test in the browser?
I already failed in webstorm: https://youtrack.jetbrains.com/issue/WEB-56836/Vitest-debugging
So what else should I add to debug tests in the browser? I tried node --inspect ./node_modules/vitest/vitest.mjs
If I try the above and have…

Nikos
- 7,295
- 7
- 52
- 88
5
votes
1 answer
Auto-import vue reactivity system in vitest for testing composables in Nuxt 3 and vitest
I am using some utils in Nuxt 3. The vue reactivity system (ref, computed, ...) is also imported directly. However, it is not the case for the tests.
Running the spec file importing a ./useBusinessValidation composable throws the error…

Lyokolux
- 1,227
- 2
- 10
- 34
5
votes
1 answer
Vitest error "TypeError: Cannot read properties of undefined (reading 'resolve')" (router.resolve)
I have a very basic test, however it throws the error below when I try to mount a component const wrapper = mount(HomeHeader). I've pasted my vite config, test, component and packages.
This error happened without updating my vite config, but I went…

Leon Gaban
- 36,509
- 115
- 332
- 529
5
votes
2 answers
Testing (ExpressJS + TypeScript + Vitest): tsconfig setup not working in Visual Studio Code
I'll just jump straight into it with an example:
I have the following (simplified) project structure:
src/
app.ts
test/
integration/
example.spec.ts
tsconfig.json
tsconfig.json
The root tsconfig.json contains following…

JC97
- 1,530
- 2
- 23
- 44
5
votes
0 answers
Stub Nuxt 3 middleware for integration tests
I search to stub a ~/server/middleware/auth server middleware, i.e. a simple true/false server auth middleware. In the present situation, I am making integration tests in the backend of the app. A route is first called, then the returned response…

Lyokolux
- 1,227
- 2
- 10
- 34
5
votes
1 answer
Vue: How to mock Auth0 for testing with Vitest
I am trying to test a Vue component with Vitest but in order to do that I need to mock auth0
Below is my Navbar.test.ts file, however when I run the test I keep getting the following error Cannot read properties of undefined (reading…

Pattle
- 5,983
- 8
- 33
- 56
5
votes
1 answer
How to write unit test for components with vitest in Nuxt 3?
I'm trying to migrate from Vue 3 to Nuxt 3. I've written unit tests for my components using vitest which are working fine in my Vue app, but the same test in the Nuxt app give me the following error:
Error: Failed to parse source for import…

mo3n
- 1,522
- 2
- 10
- 34
5
votes
2 answers
Vue Pinia function is undefined in onMounted when unit test is ran
I have a component and a Pinia store which contains a state and some actions. The code works perfectly fine in browser and in E2E (cypress) tests, but fail on unit tests. I'm using vue-testing-utils and vitest.
The store function can be called fine…

Josh Larminay
- 63
- 2
- 5
4
votes
1 answer
In Vitest, how do I assert that a console.log() happened?
The question's in the title. With Jest it was console.log = jest.fn(). How do I get and analyse the console output of the code I'm testing?

bbsimonbb
- 27,056
- 15
- 80
- 110
4
votes
1 answer
How to mock tRPC procedures with vitest and vitest-mock-extended on client side
I'm trying to mock tRPC procedures so I don't need to run the tRPC backend application to run frontend unit tests. I've tried to use vitest-mock-extended's mock and mockDeep functions. The trpc client still tries to make a request over the network,…

Ben Butterworth
- 22,056
- 10
- 114
- 167
4
votes
1 answer
Vue 3 Vitest - Test v-model on input
I am trying to create a test for a BaseInput with a v-model prop. The expectation is the component will emit the changed input. When I update the input in the Vitest framework, there does not seem to be an emit triggered.
Component
…

Spike
- 121
- 2
- 12
4
votes
0 answers
How can test watcher in vue with vitest and vue-test-utils?
I am trying to create a unit test that covers this portion of my Vue component code.
const length = ref(sizes.value[0] ? sizes.value[0].value : 0)
watch(
() => length.value,
(newLength: number) => {
current.value = 1
…

Xeyos
- 61
- 3
4
votes
0 answers
Vitest can't read CommonJS deps
I'm trying to migrate to vitest and facing a problem. One of the tests failed because it is reading a CommonJS file from one of the dependencies. The run does not fail on other tests that not using imports from CommonJS files
This is what my…

TripleN
- 41
- 3