Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
0 votes
0 answers
28 views

Why is my vi.spyOn on a mocked function not recording calls in Vitest?

I'm testing a function called compareExcelFiles in Vitest, and I'm trying to spy on the readFile function it calls internally. Despite setting up the spy with vi.spyOn, the calls to readFile aren't ...
Édor Inc's user avatar
0 votes
0 answers
39 views

How to mock class implementation used in a another class using vitest

Assume I have 2 modules that defines Repo and Service classes. // src/Repo.ts export class Repo { find() { return "hello"; } } // src/Service.ts import { Repo } from "./Repo&...
srinesha's user avatar
0 votes
0 answers
43 views

Bcrypt compare mocking issue for unit testing in Nestjs

This is my authService code and i am trying to unit test with vitest this block async validateUser( email: string, password: string, ): Promise<User | UserToSetupAccountDto | null> { ...
Sena Kaplan's user avatar
0 votes
0 answers
69 views

Nuxt registerendpoint also mocks child paths

I am just starting to write unit tests inside my nuxt project. now i have the problem that i have an endpoint path available which also has subpaths, like this: endpoint 1: /animal/dog endpoint 2: /...
Kieni's user avatar
  • 1
0 votes
0 answers
29 views

Storing mocks in separate file and importing when needed

I'm having a huge problem with Vitest and having to duplicate mocks in every test. I have tried to create a mockable.js file and import them but they are not working as expected and I'm not ...
Luke Snowden's user avatar
  • 4,176
0 votes
1 answer
186 views

Mocking graphql-request with vitest

I have been trying to mock my graphql-request with vitest. The request looks this way: const getTastyFruit = gql` query Fruit($id: String!) { fruit(id: $id) { shop { ...
Katharina Schreiber's user avatar
-1 votes
1 answer
420 views

Cannot make mocked useNavigate Work in a React, Redux, Redux-Tool-Kit application using Vitest

I build an App with Vite, React, Redux, Redux-Tool-Kit and Javascript that also involves routing and Vitest for testing. I have to build a mock of the useNavigate hook but I cannot make it work. This ...
Mawin's user avatar
  • 9
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
1 vote
0 answers
156 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 ...
ArtemBohak's user avatar
0 votes
2 answers
377 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 ...
Safvan-tsy's user avatar
0 votes
0 answers
242 views

Mock @aws-sdk/client-ses using vitest and node.js

I tried to mock aws SES sendEmail and sendRawEmail function in my vitest unit test but it is showing error undefined is not a spy or a call to a spy! my main code is emailService.ts import { SES } ...
Khaliq Anwar's user avatar
0 votes
1 answer
123 views

Issue with mocking bugsnag notify method in vue vitest

In Vitest how could I import and mock Bugsnag notify method so that I can check if it is called with a specific error in the failure workflow TypeError: [Function] is not a spy or a call to a spy! ...
Taimoor Changaiz's user avatar
1 vote
0 answers
329 views

How to mock i18n t() in vue3/nuxt3 compositionAPI with vitest?

Given the following component: <template> <WglResultList :show-sorting="true" :page-title="pageTitle" :sort-label="t('sortLabel')&...
user66875's user avatar
  • 2,610
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
0 answers
103 views

Mock Fetch Post Method

I am trying to Mock up a POST HTTP request form a pinia store but having no luck. It works within the application but I'm having trouble mocking the post request. below is my current code. I am using ...
Bryan88's user avatar
  • 427
0 votes
0 answers
295 views

ViTest Axios mock interceptors issue

I am trying to add tests for an axios interceptor that refreshes the token on a failed attempt and retries once with a new token. All tests pass apart from the one I described as: 'should refresh the ...
Polygon23's user avatar
  • 123
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
0 votes
1 answer
160 views

vue test utils v2 : cannot mock a global function

Brand new to vue testing and I'm trying to test a component that calls a global function this is imported in my app.js A simplified version of the component ConversationStatus: <script setup> ...
Ben's user avatar
  • 353
1 vote
1 answer
7k views

Vitest - how to set mock implementation in vi.mock?

Using vi.mock to create a factory of mocked named exports, if i provide an initial mock implementation to a named export it does not work. Instead, I have to manually set a mock implementation in ...
leanne1's user avatar
  • 149
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
1 vote
0 answers
467 views

Vitest - trying to mock an Object imported into a Javascript function

Ok, so disclaimer. Just starting React & Javascript from 30 years of typed languages (Java etc) so trying to get my head round it all. (First time on here also so apologies for any layout issues)....
bobbyJ's user avatar
  • 11
0 votes
1 answer
368 views

Use vi.mock() from library inside setup file

I am creating custom javascript library with content and testing part split into two entries by Vite. // library/vite.config.ts import { defineConfig } from 'vite' import path from 'path' import dts ...
Václav Procházka's user avatar
1 vote
0 answers
206 views

Vue Test Utils with Vitest: AssertionError for $routerMock.push in unit test

I'm facing an issue with my unit test for a Vue.js component using Vue Test Utils and Vitest. I have a component named TheFooter.vue, and I'm trying to test whether clicking on a link triggers ...
Sahar Elbehery's user avatar
4 votes
0 answers
732 views

How to set up vitest unit test __mocks__ for an async function

There's a module get-data-wrapper.ts, where I'd like to mock an async remote call that happens in there, but I'm not sure how to do it using Vitest. Coming from traditional OOP languages, I'm a bit ...
laketuna's user avatar
  • 4,080
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
0 votes
1 answer
724 views

How to mock node-postgres in vitest

I am trying to mock node-postgres in vitest with esm. But I am running into different problems. When I am using the Pool class in my function: import {Pool} from 'pg'; ... const pool = new Pool({...
Florat's user avatar
  • 310
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
0 votes
1 answer
307 views

Vitest failing when calling tRPC createCaller because the content contains invalid JS syntax (add "**/*.html" to `assetsInclude`)

I'm writing unit tests using Vitest and calling the tRPC createCaller in order to mock the trpc context. When running the tests it's failing on the following error: Error: Failed to parse source for ...
Yaman Ktish'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
1 vote
0 answers
30 views

spyOn function called inside function

this is my code I want to test export function y(){} export function x(){y()} now in my test i want to check if x called y. I'm already try couples of ways with no success (examples below) //not ...
eli chen's user avatar
  • 878
2 votes
0 answers
1k views

Can't mock Vue composable function with vitest

In a Vue/Nuxt project, to separate the all the business logic and data, I have created a new function with the help of Vue composition api. The custom composable function will help me to do some ...
Saeem Osman's user avatar
10 votes
3 answers
9k views

Setting a timezone in vitest

I would like to set (actually mock) a timezone in my Vitest tests to get deterministic results in all time zones they will run in. I run my tests on a local machine and also in CI/CD environments that ...
Chen Peleg's user avatar
  • 1,936
5 votes
3 answers
4k views

Mocking .env variables with Vitest

I want to mock env variables with Vitest. For now, I was able to do it this way: // test beforeAll(() => { import.meta.env.HASHNODE_URL = 'https://blog.IgorKrpenja.com'; }); // tested function ...
Igor's user avatar
  • 1,201
1 vote
1 answer
1k views

Mocking side effect function with Vitest

Not sure how I can mock a side-effect function with Vitest in Typescript Node.js project, Vitest 0.33.0. Consider this simplified example: // dummy.ts export function parent(): string { return `foo${...
Igor's user avatar
  • 1,201
1 vote
1 answer
1k views

How to reset imported file after every test in vitest?

I need to reset an imported file completely after every test. From what I understand calling vi.mock should mock my imported file using it's original contents. But it seems that any calls that should ...
user64675's user avatar
  • 356
0 votes
1 answer
681 views

Mocking dependencies with Vitest

The following code https://i.sstatic.net/vs7vp.png works when I use Jest, but it doesn't work with Vitest. I understand that Vitest operates differently in this scenario, but I haven't been able to ...
Carlos Oliveira's user avatar
4 votes
1 answer
2k views

Cannot mock static method in Vitest - error "is not a function"

I writing some unit tests in Vitest framework for my app with typescript and React, but I came across a problem with mocking a static methods. Here is my code: export class Person { private age: ...
JohnBeavis's user avatar
6 votes
1 answer
5k views

Issue with mocking Vue Router in Vitest

i'm learning Vue 3 using composition api, and im very new to Vitest aswell (i know it uses Vue Test Utils though). So, in short i'm having a problem where I mock the Vue Router, trigger click on a ...
Davidhdm's user avatar
5 votes
1 answer
2k views

How to mock dependency of dependency with Vitest

I have a test setup with vite and have a dependency of a dependency that needs some mocked options in order to run properly, I currently have it in a __mocks__/nestedDependency.js file. Also in my ...
Dimitar Nizamov's user avatar
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
557 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
5 votes
1 answer
3k views

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 ...
micurs's user avatar
  • 543
8 votes
1 answer
6k views

use "as vi.Mock" for methods from a mocked element when using typescript

When I mock vue-router with Jest, and want to mock the return value of an internal method, typescript doesn't understand that the inner method has become a mock, and can be treated as such. For ...
Liam Arbel's user avatar
8 votes
1 answer
4k views

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 ...
chiborg's user avatar
  • 28k
5 votes
1 answer
8k views

Vitest error No <functionName> export is defined on the <path>

Here i have a function which formats Weatherdata fetched from an api, i want to mock this api call to have consistent weather data to only test the pure formatting function weatherApiController.ts ...
LePa's user avatar
  • 51
1 vote
1 answer
4k views

How to mock SVG's when snapshot testing with vitest and Storybook?

I am trying to run vitest snapshot tests on Storybook stories using the composeStories Fn from @storybook/testing-react, but I keep getting the error: FAIL src/components/common/Nav/Nav.test.tsx > ...
TheKearnol's user avatar
  • 1,109
0 votes
0 answers
439 views

Why is code coverage non-zero for mocked JS imports in Vitest/Jest?

I would like to test a JS file (index.js) using Vitest. The file imports an ES6 class (service.js), instantiates it and calls a function on it. I am mocking the service in index.spec.js using vi.mock ...
Stanley Clark's user avatar
8 votes
4 answers
10k views

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 ...
Mocha_'s user avatar
  • 193