🌐
GitHub
github.com › vitest-dev › vitest › discussions › 4328
mocking different values for different test cases in a file · vitest-dev/vitest · Discussion #4328
October 18, 2023 - vi.mock('@store/actionCreators...gOrdersViewsBanner', () => { // ... }); In other words, your calls to vi.mock are not running inside the it functions....
Author   vitest-dev
🌐
Vitest
vitest.dev › guide › mocking
Mocking | Guide | Vitest
April 8, 2026 - import * as exports from './example.js' vi.spyOn(exports, 'getter', 'get').mockReturnValue('mocked') WARNING · This will not work in the Browser Mode. For a workaround, see Limitations. Example with vi.mock: WARNING · Don't forget that a vi.mock call is hoisted to top of the file. It will always be executed before all imports. example.js · ts · export function method() {} ts ·
Discussions

Browser mode: TypeError: vi.mocked(...).mockReturnValue is not a function
I started to have a random number of tests failing when I ran the whole suite of tests. It's a random number; every time it's different. e.g first run gives: Test Files 4 failed | 23 passed (27) Te... More on github.com
🌐 github.com
3
3
`vi.mock` does not use provided mock implementation
Describe the bug Using vi.mock to create a factory of mocked named exports, if i provide a default mock implementation to a named export it does not work. Instead, I have to manually set a mock imp... More on github.com
🌐 github.com
3
March 8, 2024
When using vi.mock() with In-Source Testing, mockReturnValue doesn't work
System: OS: macOS 15.5 CPU: (8) ... 140.0.7339.214 Chrome Canary: 143.0.7445.0 Safari: 18.5 npmPackages: vite: ^7.0.5 => 7.0.5 vitest: ^4.0.0-beta.15 => 4.0.0-beta.15 Note: Same behavior with vite@3 .... More on github.com
🌐 github.com
0
October 2, 2025
Mock.mockImplementation is not a function
I've tried to search every single similar issue here, unfortunately no solution worked for me. I'm currently trying to mock named exports from a module, that looks like this: module.ts export funct... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Vitest
vitest.dev › api › mock
Mocks | Vitest
March 21, 2026 - The custom function implementation in the types below is marked with a generic <T>. ... Shorthand methods like mockReturnValue, mockReturnValueOnce, mockResolvedValue and others cannot be used on a mocked class.
🌐
GitHub
github.com › vitest-dev › vitest › discussions › 8602
Browser mode: TypeError: vi.mocked(...).mockReturnValue is not a function · vitest-dev/vitest · Discussion #8602
vi.mock('../../../contexts/EntityContext'); vi.mock('../../../hooks/useAuth'); import { useEntityContext } from '../../../contexts/EntityContext'; import { useAuth0 } from '../../../hooks/useAuth'; describe('AccountSelection', () => { const mockUseEntityContext = vi.mocked(useEntityContext); const mockUseAuth0 = vi.mocked(useAuth0); const mockEntityContext = createMockEntityContext({ currentEntity: createMockEntity({ id: 'entity-123', // Keep predictable for URL matching in tests }), isSwitchingEntity: false, organizationId: 'org-123', // Keep predictable for tests }); beforeEach(() => { vi.cl
Author   vitest-dev
🌐
GitHub
github.com › vitest-dev › vitest › issues › 5358
`vi.mock` does not use provided mock implementation · Issue #5358 · vitest-dev/vitest
March 8, 2024 - // test.test.ts - this also passes but then barFn is not a mock so i cannot change its // implementation per test import { barFn } from './bar'; import { fooFn } from './foo'; vi.mock('./bar', () => ({ barFn: () => ({ bar: 3 }), })); describe.only('test', () => { // PASSES it('should return mocked bar', () => { const actual = fooFn(); expect(actual).toBe(3); }); // FAILS - barFn is not a mock it('should also return mocked bar', () => { // TypeError: barFn.mockReturnValue is not a function barFn.mockReturnValue({ bar: 4 }); const actual = fooFn(); expect(actual).toBe(4); }); }); Mac M1 Sonoma 14.3 Vitest: ├─ @vitest/coverage-v8@0.34.6 ├─ @vitest/expect@1.2.2 ├─ @vitest/runner@1.2.2 ├─ @vitest/snapshot@1.2.2 ├─ @vitest/spy@1.2.2 ├─ @vitest/utils@1.2.2 └─ vitest@1.2.2 typescript@5.3.2 ·
Author   vitest-dev
🌐
GitHub
github.com › vitest-dev › vitest › discussions › 5100
How to mock a specific method on an imported module? · vitest-dev/vitest · Discussion #5100
February 1, 2024 - import mockFsExtra from 'fs-extra'; import { describe, test, expect, vi } from 'vitest'; import * as a from './a.js'; vi.mock('fs-extra', async () => { const fsExtra = await vi.importActual('fs-extra'); return { ...fsExtra, pathExistsSync: vi.fn(), }; }); describe('a', () => { test('does path exist', () => { mockFsExtra.pathExistsSync.mockReturnValue(false); expect(a.doesPathExist()).toBe(false); }); }); Yet the test fails with the error TypeError: default.pathExistsSync.mockReturnValue is not a function.
Author   vitest-dev
🌐
GitHub
github.com › vitest-dev › vitest › discussions › 5952
vi.mock gives error "is not a function" · vitest-dev/vitest · Discussion #5952
June 21, 2024 - default.dispatch should give you a hint that the dispatch function should be defined on default: https://vitest.dev/guide/migration.html#module-mocks
Author   vitest-dev
🌐
GitHub
github.com › vitest-dev › vitest › issues › 8653
When using vi.mock() with In-Source Testing, mockReturnValue doesn't work · Issue #8653 · vitest-dev/vitest
October 2, 2025 - I want to use mockReturnValue() on a module that has been vi.mock()ed, but I can't mock the function implementation. // main_a.ts if (import.meta.vitest) { const { describe, it, expect, vi } = import.meta.vitest; vi.mock('./x.ts', () => ({ getName: vi.fn(() => '[[mocked from a]]'), })); describe('run', () => { it('enable mock', () => { vi.mocked(getName).mockReturnValue('[[mocked in test]]'); const res = run(); // If mocking is enabled, `[[mocked in test]]` should be returned.
Author   vitest-dev
🌐
GitHub
github.com › vitest-dev › vitest › discussions › 3193
vi.mocked returns a mock that I can't use without a typescript error, even though my test runs exactly as I expect. · vitest-dev/vitest · Discussion #3193
Please reload this page. Something went wrong. There was an error while loading. Please reload this page. ... There is no API in Vitest that will allow you to partially pass data to a mocked function.
Author   vitest-dev
Find elsewhere
🌐
Vitest
v0.vitest.dev › api › mock
Mock Functions | Vitest v0.34
July 11, 2023 - This is an array containing all instances that were instantiated when mock was called with a new keyword. Note, this is an actual context (this) of the function, not a return value.
🌐
Vitest
vitest.dev › api › vi.html
Vi | Vitest
Checks that a given parameter is a mock function.
🌐
Vitest
v2.vitest.dev › api › mock
Mock Functions | Vitest
November 12, 2024 - Accepts a value that will be returned whenever the mock function is called. TypeScript will only accept values that match the return type of the original function. When the mocked function runs out of implementations, it will invoke the default ...
🌐
GitHub
github.com › vitest-dev › vitest › issues › 2158
Mock + mockImplementation = undefined return value · Issue #2158 · vitest-dev/vitest
October 16, 2022 - If, in the factory function, I use vi.fn implementation, mockReturnValue or mockImplementation, instead of the desired value I obtain undefined. I've reproduced all three cases on stackblitz, using 0.24.3 version: you can find the in the mock.test.ts file. https://stackblitz.com/edit/vitest-dev-vitest-nccs1u?file=test/mock-with-implementation.test.ts ·
Author   vitest-dev
🌐
Northwestern
courses.cs.northwestern.edu › 394 › guides › mocking-services.php
Mocking Services with Vitest
When you mock a module, Vitest will replace every exported function with a mock definition that, by default, does nothing and returns undefined.
🌐
Vitest
main.vitest.dev › guide › learn › mock-functions
Mock Functions | Guide | Vitest
April 14, 2026 - The simplest way to create a mock is with vi.fn(). This gives you a function that does nothing by default (returns undefined), but tracks every call made to it:
🌐
StudyRaid
app.studyraid.com › en › read › 11292 › 352296 › mock-implementation-and-return-values
Mock implementation and return values - Mastering Vitest
Vitest allows chaining multiple mock methods for complex scenarios: ... const complexMock = vi.fn() .mockReturnValueOnce(true) .mockImplementationOnce(() => { throw new Error('Custom error'); }) .mockReturnValue(false); test('complex mock behavior', () => { expect(complexMock()).toBe(true); expect(() => complexMock()).toThrow('Custom error'); expect(complexMock()).toBe(false); });
🌐
Reddit
reddit.com › r/node › unit test not working
r/node on Reddit: Unit test not working
July 31, 2023 -

So i have a simple node service for registering new user and i want to unit test it.

I have written unit test for this service but got some error. Can anyone help me please?

Note: using express ts with postgresql(pg)

🌐
npm
npmjs.com › package › vitest-mock-extended
vitest-mock-extended - npm
April 6, 2026 - import { mockDeep } from 'vitest-mock-extended'; const mockObj: DeepMockProxy<Test1> = mockDeep<Test1>({ funcPropSupport: true }); mockObj.deepProp.calledWith(1).mockReturnValue(3); mockObj.deepProp.getNumber.calledWith(1).mockReturnValue(4); expect(mockObj.deepProp(1)).toBe(3); expect(mockObj.deepProp.getNumber(1)).toBe(4); You can also provide a fallback mock implementation to be used if you do not define a return value using calledWith.
      » npm install vitest-mock-extended
    
Published   Apr 06, 2026
Version   4.0.0