Replies: 1 comment
-
Hm, I have a feeling that this happens because the content of the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey 👋🏾
Unfortunately I can't really tell when it happens. But I get some weird side effects when I mock modules in in-source tests. Though, it does not always happen. So I'm quite confused whats the actual critical issue.
So I have a module with some function
foo
like:foo.ts
:I use that function in another module and mock it for the in-source tests:
using-foo.ts
:Till here, everything works fine.
The(bullshit)vi.mock
inside thedescribe
block is already an approach to "scope" the mocking somehow.But if I now have a another module that uses
foo
and has no in-source testing (dunno if it actually makes a difference), I get issues.also-using-foo.ts
:also-using-foo.spec.ts
:Now I get errors because
foo
is somehow mocked by thevi.mock
and returnsundefined
(so thelength
property does not exist). If I comment out thevi.mock
in the other tests, these tests here just work fine.My Vitest configuration is actually quite simple. I thought especially the
restoreMocks
should help (while being useful in general).As I read from the documentation, per default Vitest runs test modules isolated from each other.
But if Vitest runs the
also-using-foo.spec.ts
, thenimport.meta.vitest
is defined. So in case theusing-foo.ts
module would be loaded (also it isn't needed here), the if-condition would be true. So in theory the in-source tests get executed. Which would include thevi.mock
. But I don't know how this actually works. I makes no difference if I would put thevi.mock
in any of thebefore*
hooks either. 🤷🏾Anyone experiencing the same issues? Are there some best practices how to use module mocking for in-source tests?
Thank you very much for any help in advance! 🙏🏾
EDIT:
Weird enough, if I provide a factory for the module mock it won't be used. Like in
using-foo.ts
in the in-source tests where thevi.mock
call is:I get still issues in
also-using-foo.spec.ts
that I getundefined
as return values byfoo
. 🙇🏾♂️Beta Was this translation helpful? Give feedback.
All reactions