-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
cannot spy on a Getter from a Service that is mocked - regression since version 0.21.x #1899
Comments
Interesting. So, automocking doesn't keep getters now? @simon-abbott what do you think? |
It's a wild guess, I'm not sure if it's related to automocking or not but it seems to be the only PR related to mocking that was released in version |
Definitely related to automocking. The property exists on original module, but after calling |
Yes, the automocking change did result in getters being skipped, and there is a TODO for making it work: vitest/packages/vitest/src/runtime/mocker.ts Lines 213 to 215 in 88d5764
I copied much of Jest's automocking logic, so we could be as close as possible, which includes not mocking getters. I don't remember the exactly reason why this change was necessary, but if I have some downtime soon I'll take another look at it. |
@ghiscoding I have started a fix. Expect a PR in the next couple hours. |
awesome, my use case is really the same as the provided stackblitz, so if it works with that then it will most probably work on my side too 😉 Thanks for the quick response and feedback |
PR up: #1903. Sorry about that! |
Side note, there's a similar PR opened by a new Jest contributor (so take it with a grain of salt) for the same subject Getters/Setters Mock - Jest - Mocking of getters/setters on automatically mocked classes does not work |
Describe the bug
I have a Vue Pinia Store implementation that I want to test and I want to mock an Service instead of using the actual service, it works fine for nearly everything except when trying to spy on a Getter where it throws an error saying that
"x does not exist"
, if however I rename my Getter to a function fromget x()
togetX()
then it works as intendedThis seems to be a regression since it was working properly in older Vitest version 0.17.x. I'm upgrading my Vue 3 project to Vite 3 and I started updating to latest Vitest version but the highest I could go before I get this new error was version 0.20.x and anything higher than that would throw the error. I also started upgrading to Vite 3 and now even older version of Vitest throws the error (probably because Vitest 0.17.x wasn't working properly with Vite 3).
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-od71ph?file=test%2FsolutionDataService.test.ts,services%2FsolutionDataService.ts&initialPath=__vitest__
Run the Service test
solutionDataService.test.ts
Simple code
Error thrown
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ FAIL test/solutionDataService.test.ts > service with getter Error: errorsAndWarnings does not exist ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
If we modify the
package.json
of that same stackblitz with older version (as shown below) then everything passesif we try to bump only Vitest to
0.21.0
(fixed version), then it starts failing again with the error, so it looks like the regression was introduced in version0.21.0
and just guessing that it might be caused by PR #1786 (very wild guess just by looking at the code)System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: