How to perform unit tests on an 'inline composable' #2369
Unanswered
RonaldJair19
asked this question in
Q&A
Replies: 1 comment 1 reply
-
I don't think this is possible, because spying in JS doesn't work like this. it('should mock the function fnInside', () => {
// this is the setup of your component
const factory = () => {
const fnInside = () => 10
const fnOutside = () => fnInside()
return { fnInside, fnOutside }
}
// this is the mount(Component)
const obj = factory()
// override fnInside
obj.fnInside = vi.fn().mockReturnValue(1)
expect(obj.fnInside()).toBe(1) // the override works and returns 1
expect(obj.fnOutside()).toBe(1) // fails, returns 10, because it uses an internal reference to fnInside and not our override
}) So you won't be able to do what you attempt. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Good morning/afternoon/evening community,
I am having problems to perform unit tests to an 'inline composable'. Below I will present an example code to what I mean by this concept:
When trying to mock the 'methodTest' function as follows:
Or like this:
And I call the 'initialize ' function within my test, the mock does not work and continues to take the value that is set within composable, i.e. the 'methodTest' function continues to return 'Hello word'.
Regards and thank you very much,
Beta Was this translation helpful? Give feedback.
All reactions