Questions tagged [vitest]
Vitest is a blazing-fast unit test framework powered by Vite.
vitest
853
questions
-1
votes
0
answers
46
views
Vitest: Cannot read properties of undefined (reading 'includes')
I'm testing with Vitest an Ionic-React-Redux (-toolkit) app.
I run the test with the recommended command:
"test.unit": "vitest"
This is the App.test.tsx created by default that ...
0
votes
1
answer
13
views
Vitest not properly interpreting TypeScript
I have the following setup test:
import { describe, it } from "vitest";
import { init, initServer } from "../src";
import dotenv from "dotenv";
dotenv.config();
...
0
votes
0
answers
11
views
react-dropzone onDrop is not triggered programmatically when using unaccepted file type
I'm using react-dropzone in my React project, and I have it set up to only accept .stl and .zip files. Currently I'm testing other file types to ensure that an error is shown when the user uploads an ...
0
votes
1
answer
28
views
Vue Testing: Triggering Checkbox not working
I'm trying to test if a checkbox has been checked with Vue test utils and Vitest, but the test always fails and I don't understand why.
Component.vue:
<template>
<input type="...
0
votes
1
answer
54
views
How can one write unit tests for CSS in a Svelte component?
I have a HelloWorld Svelte component as below:
//
// HelloWorld.svelte
//
<script>
export let name = 'world';
</script>
<h1>Hello {name}!</h1>
<style>
h1 {
...
0
votes
0
answers
17
views
Not able to mock mutation in Redux Toolkit Query
Below is the code I have for one of the query in Redux Toolkit Query
workflowQuery.ts
export const workflowQuery = createApi({
reducerPath: 'workflowQuery',
baseQuery: await fetchBaseQuery({
...
1
vote
1
answer
48
views
Vue how to mock a ref value
I am writing a modal component in vue 3:
// MyModal.vue
<script setup>
const emit = defineEmits(['close'])
const modalDialog = ref(null)
const close = () => {
emit('close')
...
1
vote
0
answers
34
views
Vitest/React Testing Library - Error: Test timed out in 5000ms
In attempting to test setInterval() within the start_new_game() function nested within the <Simon /> component the following error is being raised:
Error: Test timed out in 5000ms. If this is a ...
0
votes
0
answers
13
views
when debugging vitest unittest with vscode the Debug Console cannot access imported modules
I'm using vitest+vue3. When launching the debugger with launch.json pressing F5, in my Debug Console I can use modules imported by the script i'm debugging no problem.
but when I want to do the same ...
0
votes
0
answers
21
views
TypeError: Cannot add property widgetRef, object is not extensible
Using vue/test-utils v1 I was modifying the $refs like this
const wrapper = factory();
wrapper.vm.$refs.widgetRef = {
actionStep: vi.fn(),
redraw: vi.fn(),
};
After moving to v2, now its throwing ...
0
votes
0
answers
13
views
Unit Test Repeatedly Fails When Trying To Remove Line Item From Cart (Vitest)
I'm relatively new to the world of writing unit tests and using Vitest so my issue might be quite obvious to someone more experienced but I can't for the life of me get the third test to actually ...
0
votes
0
answers
20
views
Failed to resolve import "#app/composables/script-stubs (Vitest)
I am receiving the error 'Failed to resolve import "#app/composables/script-stubs"' inside the .nuxt folder in the imports.d.ts file
My code is like this and I've tried importing everything.....
0
votes
0
answers
13
views
Trouble Testing Error Handling in Next.js Component with TypeScript and Vitest
Environment:
"next": "14.2.3",
"vitest": "1.6.0",
"@testing-library/jest-dom": "6.4.5",
I'm trying to write tests for a Next.js component ...
0
votes
1
answer
40
views
How to test v-text-field rule placeholder's text in v-messages__message?
<v-text-field
density="compact"
v-model="textMaterial"
:rules="textMaterialRules"
label="Text Material"
></v-text-field>
This is a ...
0
votes
1
answer
38
views
How to ignore lines of code from coverage in vitest?
I have my unit tests setup with vitest and I am looking for a way to ignore specific lines of code from vitest coverage.
Looking for something similar to /* istanbul ignore next */.
Is there something ...
0
votes
0
answers
29
views
Error: Target container is not a DOM element. Test React Vitest
Issue:
An "Error: Target container is not a DOM element" occurs when running tests for a React component that utilizes Firebase to add documents.
Observed Behavior:
The application ...
0
votes
0
answers
26
views
Cannot find module "jsx-dev-runtime" when running Vitest (Next.js)
I am trying to add Vitest to my Next.js-project and have followed their guide. However when I run npm run test, I get the following error message:
Error: Cannot find module '/path/to/repo/node_modules/...
1
vote
1
answer
34
views
How to unit test vue beforeRouteUpdate hooks
I have the following vue component, which uses onBeforeRouteUpdate hook to call the init() method when the route param changes (test/a -> test/b).
The following stackblitz instance shows the output ...
0
votes
1
answer
36
views
Configuration Vitest in Nuxt 3
I have a large project made with Nuxt 3, Vue, Pinia, Axios, and TypeScript.
I need help with configuring Vitest to work in my project.
I've had many problems. For example, I wrote a test and it didn't ...
0
votes
1
answer
35
views
How do I mock a function in react-modal using Vitest?
I am writing a test for a module that uses react-modal to open a modal. React-modal requires that you set the root element for accessibility reasons.
if (typeof document != "undefined") {
...
0
votes
0
answers
20
views
How to mock single imported variable in vitest for different test cases
I have defined a single constant, who's definition depends on DOM:
// utils.ts
export const isRootPresent = Boolean(document.querySelector('#root'));
My react app uses it do show/hide text:
// App....
-1
votes
0
answers
38
views
How to mock a custom hook with a default export in vitest
I've been trying to mock a custom hook but continuously face the same error. I think I am probably missing a fundamental concept about named exports and default exports here.
Consider the following ...
1
vote
0
answers
29
views
Vuetify v-form Form Validation Not Working in Vitest
I'm currently writing tests for a Vue3 + Vuetify website with Vitest.
The website uses Vuetify components and one of the components I'm testing includes a v-form component. Here is the component code:
...
0
votes
0
answers
28
views
Vitest + @testing-library/react Issue
currently I’m stuck on this one. so i'm implementing a unit test in my turbo repo app with react remix and i'm creating a unit test for this component
/apps/my-portal/routes/signin.forgot.jsx
import { ...
0
votes
1
answer
41
views
ViTest | Cannot import `.css` files that are compiled from `.scss`
I use some .scss files as part of a re-usable ES6 module (ESM). As part of the build process I am running yarn sass -I node_modules src:dist to compile all of these files to .css and they are output ...
0
votes
0
answers
27
views
Issue typing custom hook parameter on vi.spyOn
I'm using Vite for a React + TypeScript project.
I'm currently trying to test some parts of the app using Vitest and I'm facing this problem when I'm tyring to mock data that I receive from a custom ...
-1
votes
0
answers
22
views
How to use different mocks for each test with vi.mock
I am using vitest for mocking in my project -
import { screen, fireEvent } from '@testing-library/react';
import { renderComponentWithProviders as render } from 'common/tests/renderWithProviders';
...
0
votes
0
answers
34
views
Testing with vitest returns AssertionError: expected "spy" to be called 1 times, but got 0 times
can someone explain why exactly the vitest below is failing given the implementation provided.
import { createCacheKeyFromArgs, memoize } from './memoize';
const mocks = {
add: vi.fn((x, y) => x ...
0
votes
0
answers
32
views
Test Cases on Redux Toolkit Query not giving expected result
I am writing test cases for redux toolkit query but it is not giving me expected results.
This is my api.ts
export const notificationApi = createApi({
reducerPath: 'notificationApi',
baseQuery: ...
0
votes
2
answers
47
views
supertest request body is undefined
I want to test my express server POST endpoint using supertest. Everything works fine in postman, but when I try to pass in body parameters into the test, as can be seen in the code snippets below, it ...
0
votes
0
answers
28
views
Vitest, beforeEach and concurrent
I am writing some tests in vitest and I wonder if something like this is safe:
describe.concurrent("Let's do some testing:",
() => {
let ctx;
beforeEach(async
() => {...
0
votes
2
answers
57
views
Mocking useStorage() using @nuxt/test-utils
The goal is to write a test for a function that uses KV storage, under the hood provided by Nitro, in the Nuxt app. On the client side useStorage() is automatically imported. When writing a test, ...
0
votes
1
answer
66
views
Properly mocking a composable function with vitest
In my Vue 3 app, I use the pattern of having a composable that contains methods that will handle fetching some data from various APIs. The reason they're composables rather than simple functions is ...
0
votes
1
answer
73
views
'TypeError: Cannot read properties of undefined' when accessing state member only when running on vitest
I am trying to write unit tests for my React application, built with react-query and ky. I am constantly faced with the error Error: Uncaught [TypeError: Cannot read properties of undefined (reading '...
0
votes
1
answer
62
views
Prisma + postgresql extensions on different (database, not Prisma) schema
I'm working on writing a Prisma extension to support pgvector. All seems fine, and I've moved on to writing automated test cases. I've been following this methodology for vitest, which works by ...
0
votes
0
answers
19
views
Getting an error while executing test case in React Vite
I am using React with Vite. And this is the piece of code for which I am writing test cases -
import { otelLogError } from 'instrumentation-logger';
const {
data: notificationResponse,
error,
...
0
votes
0
answers
36
views
useSearchParams Returning null or undefined in Next.js Component Tests with Vitest
Problem
I'm working on a Next.js project and using Vitest for testing. I need to mock the next/navigation module, specifically useRouter and usePathname, while keeping the original implementation of ...
1
vote
0
answers
25
views
Merge vitest and playwright coverage repors
I run vitest coverage via:
$ VITE_COVERAGE=true vitest --ui --coverage.enabled=true
and I get a really cool interface with all my vitest tests and coverage.
Now, I'd like to keep using this interface,...
0
votes
0
answers
26
views
Coverage does not work when using Istanbul
If I run vitest run --coverage when using default's v8 system, everything works, and I get a proper text table with the coverage. But if I use now Istanbul instead, I get an error:
> 2024-03-...
0
votes
0
answers
28
views
How to test aria-expanded attributes
I am writing a test for a simple svelte component. It goes like this:
it('opens and closes correctly', async () => {
render(Menubar, { menuData: menu_with_items });
const menuButton = screen....
-1
votes
0
answers
14
views
Is it wrong to use a node module package directly in unit tests instead of mocking them
I'm using vitest to write some unit tests in my react app, and I've been wondering why we don't just use the node_module packages when necessary directly instead of mocking them?
I have included the ...
0
votes
1
answer
30
views
How to write test for a js function with vitest mocking side effect function
I'm trying to write a test for a function in my Node.js/Express application using Vitest. The function generateData processes some request data, calls two other functions (getParsedData and ...
0
votes
0
answers
8
views
Mock the currentTime in useSyncExternalStore
I'm trying to mock the current Time (Temporal.Now.plainDateTimeISO()) with
vi.setSystemTime(new Date('2024-06-01T11:00:00'));
but every time I run this test the mocked time is ignored.
store:
...
1
vote
0
answers
80
views
I get error, "Error: Expression expected" when I run my test in my Vite application
My test fails for some weird reason.
Below is the file that contains the whole code, I am not sure why it fails, even when I used different matchers and attributes that I expect to work like "...
0
votes
1
answer
49
views
Getting "Expression Expected" when trying to run a test using CreateRemixStub and Vitest
Whenever I run the test executing "npx vitest", I get:
My app is a Shopify Hydrogen v2 running in top of Remix Run. I want to implement a test for the AddToFavoritesButton component in ...
-1
votes
0
answers
22
views
Mocking multiple functions in vitest
I have a component that shows data in a grid if the API returns data, and if no data is returned, it shows a message:
export default async function Operations({
searchParams,
}: {
searchParams: ...
0
votes
0
answers
19
views
Can't use flowbite Button into vitest+jsdom?
I'm trying to build basic component testing via vitest and jsdom, so I created vitest.config.js containing:
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
...
2
votes
1
answer
50
views
useLoaderData must be used in a data router - Vitest - npm run test
In attempting to test React components that implement the React Router API, I'm getting an error while running tests at the same time. If either test is commented out the error is not raised and the ...
0
votes
1
answer
47
views
In Svelte with Vitest is there a way to mock the browser value in $app/environment
Seems like there should be a way to modify the value of browser. Typescript doesn't allow assignment eg browser = true; yielding error: Cannot assign to 'browser' because it is an import.ts(2632)
...
0
votes
1
answer
39
views
CANNOT override Service in NestJS/Vitest
We are implementing e2e testing in an application using NestJS/Vitest/Prisma.
I used overrideProvider to Mock ItemsService in my ItemsController test, but it doesn't work.
The test code and test ...