Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
0 votes
1 answer
43 views

Mock not being called in React Vitest App test

I have this React component: import React, { useState, useEffect } from "react"; import { BrowserRouter } from "react-router-dom"; import "./App.css"; import Home from &...
Camden Tadhg's user avatar
3 votes
1 answer
206 views

Is it possible to implement the AuthState TypeScript Interface in the react-oidc-context Node module for Next.js testing purposes?

We have a Next.js app that uses the react-oidc-context Node module, as we are using ADFS for authentication. Currently, I am trying to write unit tests using Vitest, and am getting this error: ...
Brian's user avatar
  • 1,969
-1 votes
1 answer
875 views

Vitest - Mock React Component only in one test

I want to mock a Component named Child in Parent. All tests should use the original Child component except one test should use a Mock. how do I achieve that? I tried working with vi.mock('./path/to/...
sanyooh's user avatar
  • 1,621
0 votes
1 answer
502 views

React Vitest mocking api fetch returns: TypeError: Cannot read properties of undefined (reading 'json')

In a personal Vite React project I want to display a list of anime on a page from a Rapid-Api api. I have a displayAnime function in the AnimeInfoPage component (/:id) I am trying to test: const ...
Raphael40's user avatar
2 votes
1 answer
1k views

Set different mockReturnValue for each test in Vitest

In my unit test for my react app I am trying to mock a custom hook that returns objects with data that gets displayed in the component and determines whether some other elements get rendered. I ...
Tuyen Thanh's user avatar
0 votes
1 answer
2k views

Mocking useParams from react-router-dom in Vitest returns {} instead of actual mock

I'm working on a React project where I'm using react-router-dom. For testing purposes I'm using Vitest to mock the useParams hook. However, after mocking useParams and adding a console log to check ...
Hubert Kuzdak's user avatar
2 votes
1 answer
3k views

Vitest: Mock a provider's custom useContext in a consumer component in React

The premise I have the following components: // providers/SomeContextProvider.tsx import { createContext, ReactNode, useContext } from 'react'; const SomeContext = createContext(''); export type ...
Dimitris Karagiannis's user avatar
-1 votes
1 answer
328 views

How to mock a dispatch made in a useState?

I have a component that displays a skeleton when a status is "idle", and dispatch a request with react-redux to get data. I am writing the test file for this component, and I want to render ...
Johan's user avatar
  • 2,854
1 vote
0 answers
281 views

How to Resolve "Cannot access 'mockKeycloak' before initialization" Error in Jest and VITest?

I'm writing tests using Jest and VITest for a React application. I've encountered an error related to the initialization order of mock functions and variables. The error message is "Cannot access ...
CoolLife's user avatar
  • 1,479
2 votes
0 answers
2k views

focus-trap-react error when testing with vitest and jsdom

I am having the following error when trying to test components with vitest running js-dom environment that use focus-trap-react . Error: Your focus-trap must have at least one container with at least ...
Dimitar Nizamov's user avatar
0 votes
1 answer
558 views

Getting an error when I run react(vitest) unit test cases. I got to know It's due to the local image paths

Error: stderr | unknown test Error: Error: connect ECONNREFUSED ::1:3000 at Object.dispatchError (E:\customer-dashboard\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:63:19) at Request. (E:\...
BCK's user avatar
  • 1
4 votes
0 answers
5k views

How to mock useState hook and check if setState have been called with vitest?

I'm migrating from jest to vitest at my React application. I'm testing a component which must call the setState function in certain situations and with jest I did something like: it('Component ...
Yuri Henrique's user avatar