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
Bug: Mocking not working when setup() is defined #1869
Comments
Facing the exact same issue with test-utils 2.2.3 I think bug was introduced in one of the recent patch version, will try to downgrade to 2.2.2 or 2.2.1 in the meantime |
Yes, this comes from internal changes in Vue core v3.2.45, which we try to workaround in VTU but some cases are tricky, as Vue no longer allows to modify the internal state of a setup component. In the meantime, you can downgrade to Vue v3.2.44 |
Oh that interesting, because we are still using Vue v3.2.41 and this issue appeared after updating test-utils to v2.2.3. I just did a downgrade to v2.2.1 and the error is now gone. |
I may have found a way to make this use-case work with Vue v3.2.45 |
Hey @cexbrayat,
|
@JeremCafeyn Happy to hear that 👍 For global mocks, can you provide us a small repro online using https://stackblitz.com/edit/vitest-dev-vitest-qoiwy3?file=package.json&initialPath=__vitest__ ? I'll take a look to check if this is a VTU issue. |
we still have the same error. downgrading to 2.2.2 fixes it immediately... using a combination of options api and composition api in our project. and since 2.2.3 we get this problem: global mocks just disappear (in our case, we 'enhanced' the mount/shallowMount with mocks for all vue-i18n methods, so all our mount/shallowMount have this mocked vue-i18n methods, even adding a mocked $router just ... disappears...) |
@stefanlivens Can you provide us a small repro online using https://stackblitz.com/edit/vitest-dev-vitest-qoiwy3?file=package.json&initialPath=__vitest__ ? |
Sure! and thanks for your reply. Here you go: I included a setup file in the test folder to change the global config, and change also the vitest.config. |
@JeremCafeyn If you use |
Remove the
to
and
to
and |
Have the exact same scenario as #1869 (comment), have mixed options API component with a Cannot reproduce with a bare vitest stackblitz, so must be something on my dev environment?! :/ @stefanlivens appreciate if you have more info on it Cheers 👋 Stack
EDIT: screenshots from running on my env with a patched
EDIT2: I can also confirm that downgrading to |
@cexbrayat it seems that the patch introduced at That being said, I can reproduce it using latest Any help is appreciated! Cheers 👋 |
Hmm that's strange. I'm not surprised that having a component with both composition and options API has a weird behavior, as the VTU codebase now distinguishes the 2 cases. More than that, I'm surprised that this can only be reproduced with Jest. If we don't have a way to reproduce this in the VTU test suite, this is going to be hard even to just take a look. Ideally, you should try to move to a component with only a setup or a component with plain Option API. Trying to fix this "mix component case" in VTU is going to add even more complexity to something that is already quite tricky I'm afraid. |
At the scale of the app I'm working that's not doable ATM. Also
With that, I fully agree. You're already relying on Another weird part: the reason you add the Lines 481 to 485 in a48712d
EditI've patched my fork of test-utils as shown bellow and all tests pass. I know that it's increasing complexity/hackery, but on the other hand, we were already accessing internals anyways. Of course that before making a PR, I would like to add a test case to the codebase where the previous code failed but haven't got any success with that. - if (hasSetupState(this)) { }
+ if (hasSetupState(this) && this.$.setupState.__isScriptSetup) { } |
hi guys, i simply could not reproduce it in the stackblitz setup. and i missed your call to help, my bad However, i can confirm v2.2.7 fixed the issue for me! |
Hey @stefanlivens no worries about that! You already helped a lot by demonstrating that I was not the only one with the issue! That's what drove me to find a solution, otherwise, I would just give up and accept as a fact that the problem was with my setup! |
Describe the bug
I'm trying to mock the vuei18n $t. But this is not working when using the setup composition api.
When disabling the setup, it works as expected. I also tried with other function names like $test.
Strange thing is, when using a name without the dollar sign, it also works just normal.
To Reproduce
index.vue
test file
Expected behavior
Should also works when using the composition api.
Related information:
@vue/test-utils
version: 2.2.3Vue
version: 3.2.45node
version: v18.2.0npm
(oryarn
) version: 8.10.0The text was updated successfully, but these errors were encountered: