-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Utility to automock class instance #4001
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
Comments
I would be very interested in this feature !! In my particular case, I'm using Vitest to unit test Vue.js components. And I often need to instanciate a class to It would also be very useful to be able to retrieve the mocked methods of this mocked class instance, to specify a behaviour if needed, depending on the test case. import { Class } from 'module'
vi.mock('module')
// @ts-ignore-error Ignore constructor argument errors
const mockInstance = new Class()
mockInstance.someMethod.mockReturnValue('mocked value');
expect(mockInstance.someMethod).toHaveBeenCalled(); |
Is this already possible with https://stackblitz.com/edit/vitest-dev-vitest-hnt4sl?file=src%2Fsome.test.ts //
// some.test.ts
//
import { vi, test, expect } from 'vitest';
test('repro', async () => {
const { SomeClass } = await vi.importMock<typeof import('./some-class')>('./some-class');
const someInstance = new SomeClass();
// type check works
vi.mocked(someInstance.someMethod).mockReturnValue('hello');
expect(someInstance.someMethod(1234)).toMatchInlineSnapshot(`"hello"`);
});
//
// some-class.ts
//
export class SomeClass {
someMethod(x: number) {
return String(x);
}
} |
Add a constructor with parameters to SomeClass. You'll get a type error
unless you provide it when calling the mocked constructor.
That's not too bad, you can add a ts-ignore or cast to suppress it. But
that isn't elegant. We could use a simple helper similar to vi.mocked.
const instance = vitest.constructMocked(SomeClass)
constructMocked would call new on the argument.
…On Wed, Apr 17, 2024, 4:53 PM Hiroshi Ogawa ***@***.***> wrote:
Is this already possible with vi.importMock? This should return
"automock"-ed module without actually setting up module mocking. I made a
simple example here:
https://stackblitz.com/edit/vitest-dev-vitest-hnt4sl?file=src%2Fsome.test.ts
//// some.test.ts//import { vi, test, expect } from 'vitest';
test('repro', async () => {
const { SomeClass } = await vi.importMock<typeof import('./some-class')>('./some-class');
const someInstance = new SomeClass();
// type check works
vi.mocked(someInstance.someMethod).mockReturnValue('hello');
expect(someInstance.someMethod(1234)).toMatchInlineSnapshot(`"hello"`);});
//// some-class.ts//export class SomeClass {
someMethod(x: number) {
return String(x);
}}
—
Reply to this email directly, view it on GitHub
<#4001 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAE6WM6VOZMLQTB6EHLHAA3Y54DQZAVCNFSM6AAAAAA32DZXKSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANRSG4ZDMMBYGY>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
I see. Your concern was the type error on construction. Then I'm not sure if it's really important features to just help silencing type errors. EDIT: Well, I actually thought it would be a neat feature if we can expose vitest/packages/vitest/src/runtime/mocker.ts Line 282 in c84113f
|
I get this error while trying to instanciate the class your way :
The class is the only and default export of the module (single file class). |
Ok, it works this way : //
// some.test.ts
//
import { vi, test, expect } from 'vitest';
test('repro', async () => {
const SomeClass = (await vi.importMock<typeof import('./SomeClass')>('./SomeClass')).default;
const someInstance = new SomeClass();
// type check works
vi.mocked(someInstance.someMethod).mockReturnValue('hello');
expect(someInstance.someMethod(1234)).toMatchInlineSnapshot(`"hello"`);
});
//
// SomeClass.ts
//
export default class SomeClass {
someMethod(x: number) {
return String(x);
}
} Thx for the tip @hi-ogawa !! |
👍 |
We can expose something like this: const SomeClass = vi.mockObject(OriginalClass)
const instance = new SomeClass() // Mock<OriginalClass>
expect(SomeClass).toHaveBeenCalled() |
Clear and concise description of the problem
As a developer using vitest I want an easy way to create mock instances of a class. While I can us
vi.mock('module')
to automock the entire file I have to jump thru hoops to get a mock instance, mainly to avoid TypeScript errorsvi.mock
will mock the whole module, so if you wanted another exported value to use the real implementation you need a factory to import the real implementation.Suggested solution
Alternative
No response
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: