Skip to content
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

Closed
6 tasks done
ghiscoding opened this issue Aug 22, 2022 · 8 comments · Fixed by #1903
Closed
6 tasks done

Comments

@ghiscoding
Copy link
Contributor

ghiscoding commented Aug 22, 2022

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 from get x() to getX() then it works as intended

This 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

vi.mock('../services/solutionDataService');
import SolutionDataService from '../services/solutionDataService';

test('service with getter', async () => {
  vi.spyOn(SolutionDataService, 'errorsAndWarnings', 'get').mockReturnValue({ msg: 'something' });
});

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 passes

"devDependencies": {
  "@vitejs/plugin-vue": "^2.3.3",
  "@vue/test-utils": "latest",
  "jsdom": "latest",
  "vite": "^2.9.15",
  "vitest": "^0.20.3"
}

if 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 version 0.21.0 and just guessing that it might be caused by PR #1786 (very wild guess just by looking at the code)

System Info

System:
    OS: Windows 10 10.0.19043
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
    Memory: 13.52 GB / 31.75 GB
  Binaries:
    Node: 16.17.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 8.15.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 104.0.5112.82
    Edge: Spartan (44.19041.1266.0), Chromium (104.0.1293.54)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @vitejs/plugin-vue: ^3.0.3 => 3.0.3
    vite: ^3.0.9 => 3.0.9
    vitest: ^0.22.1 => 0.22.1

Used Package Manager

yarn

Validations

@ghiscoding ghiscoding changed the title cannot spy on a Getter from a Service that is mocked - possible regression since version 0.20.x cannot spy on a Getter from a Service that is mocked - regression since version 0.20.x Aug 22, 2022
@ghiscoding ghiscoding changed the title cannot spy on a Getter from a Service that is mocked - regression since version 0.20.x cannot spy on a Getter from a Service that is mocked - regression since version 0.21.x Aug 22, 2022
@sheremet-va
Copy link
Member

Interesting. So, automocking doesn't keep getters now? @simon-abbott what do you think?

@ghiscoding
Copy link
Contributor Author

ghiscoding commented Aug 23, 2022

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 0.21.0 which started to regress so there goes my wild guess... You guys can try the simple stackblitz repro (2 small files) and see for yourself, switching the versions in package.json will make it fail (newer version) or make it work (version <= 0.20.3)

@sheremet-va
Copy link
Member

It's a wild guess, I'm not sure if it's related to automocking or not but you it seems to be the only PR related to mocking that was released in version 0.21.0 which started to regress so there goes my wild guess... You guys can try the simple stackblitz repro (2 small files) and see for yourself, switching the versions in package.json will make it fail (newer version) or make it work (version <= 0.20.3)

Definitely related to automocking. The property exists on original module, but after calling vi.mock without factory it is removed.

@simon-abbott
Copy link
Contributor

Yes, the automocking change did result in getters being skipped, and there is a TODO for making it work:

// TODO: Mock getters/setters somehow?
if (descriptor.get || descriptor.set)
continue

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.

@simon-abbott
Copy link
Contributor

@ghiscoding I have started a fix. Expect a PR in the next couple hours.

@ghiscoding
Copy link
Contributor Author

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

@simon-abbott
Copy link
Contributor

PR up: #1903. Sorry about that!

@ghiscoding
Copy link
Contributor Author

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

@github-actions github-actions bot locked and limited conversation to collaborators Jun 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants