New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add vi.createMockFromModule
(Like jest.createMockFromModule
)
#5482
Comments
Is this maybe a bug? Or if it's causing some infinite loop, that needs to be fixed at least? |
Not an infinite loop, you are just getting your own module namespace object as far as I can tell. And I think |
vi. createMockFromModule
(Like jest.createMockFromModule
)vi.createMockFromModule
(Like jest.createMockFromModule
)
It's called vitest/packages/vitest/src/runtime/mocker.ts Lines 437 to 455 in e4e939b
Btw, can you setup a small reproduction to illustrate what you want to do with a concrete code? That would be helpful anyway as a test case if it's implemented. Also it would also help for us to suggest a workaround with what's currently possible. One idea just came to my mind is to still use // use `setupFiles` to setup mock for all test files
import { vi, beforeEach } from "vitest"
import * as fooLib from "./foo";
// setup auto mocking for a whole module
vi.mock("./foo");
beforeEach(() => {
// then customize some named export
vi.mocked(fooLib).bar.mockImplementation(() => 'qux');
}); |
Clear and concise description of the problem
I want to create a manual mock (
__mocks__
), but only specialize parts of the default mock that is automatically created by vitest, in Jest I could do:But
createMockFromModule
is not available in Vitest, andimportMock
is not the same, as when used inside a manual mock, it just ends up importing the manual mock itself cyclicly.createMockFromModule
always returns the automatically created mock object even if there is a manual mock.Suggested solution
Add
vi.createMockFromModule
or under a different name that better fits it's async nature in vitest, that acts likevi.importMock
but always return an automatically created mock even if there is a manual mock.Alternative
Manually mock everything in the manual mock, which is cumbersome and harder to maintain.
Additional context
Encountered while migrating some tests from Jest to Vitest due to Jest's ESM issues.
#3718
Validations
The text was updated successfully, but these errors were encountered: