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

Jest is no longer able to spy on methods #309

Closed
TheDutchCoder opened this issue Jan 26, 2021 · 9 comments · Fixed by #310
Closed

Jest is no longer able to spy on methods #309

TheDutchCoder opened this issue Jan 26, 2021 · 9 comments · Fixed by #310
Labels

Comments

@TheDutchCoder
Copy link
Contributor

I think the wrapper no longer exposes methods on the prototype or something, because when I want to spy on a method (e.g. wrapper.vm.send) with jest.spyOn(wrapper.vm, 'send') Jest throws a type error: TypeError: object.hasOwnProperty is not a function.

This makes it impossible to test some component internals (like methods that format data and send it off to a state machine, in this case).

@cexbrayat
Copy link
Member

Hi there! Thank you for opening an issue.

That's weird, I have similar code in all my apps, and it does work well for me 🤔

Can you provide us a small reproduction so we can investigate?

@TheDutchCoder
Copy link
Contributor Author

Sure!
I use state machines (xstate) in my components which is a composable that exposes a send method like so:

setup (props) {
    const { send, state } = useMachine(assetsMachine)

    const doThing = () => {
      send('hello')
    }

    return {
      send,
      doThing,
    }

However, trying to spy on send yields that error, even though wrapper.vm.send logs as a function.
I have a feeling the issue lies with composable functions.

@cexbrayat
Copy link
Member

Can you push your repro on a Githup repo please? We'll need to know how your project is set up.

Ideally just a small component like this one, and a failing unit test with the error 👍

@TheDutchCoder
Copy link
Contributor Author

I'll try to create a codesandbox, it would take hours extracting this from my project.

@TheDutchCoder
Copy link
Contributor Author

That's not going to work. CodeSandbox can't even run Jest at the moment.

The issue seems to be that wrapper.vm is a Proxy object and jest.spyOn can't find the method on wrapper.vm as it doesn't have a hasOwnProperty method.

@cexbrayat
Copy link
Member

I managed to repro your issue, but I don't have an obvious fix right now. This may need some thinking.

@TheDutchCoder
Copy link
Contributor Author

Thanks a ton, it's driving me up the wall haha!

cexbrayat referenced this issue in cexbrayat/vue-test-utils-next Jan 26, 2021
This allows Jest to spy on the proxied vm
Fixes #309
@per-toyra-stratsys
Copy link

We're having the same issue. Is there a time plan when this fix will be released?

@cexbrayat
Copy link
Member

I have a fix but it's blocked by a change we need in Vue itself (vuejs/core#3105).
So we need Evan to take a look at that PR and merge it, which should hopefully happen in the next weeks.
We can then land the fix and cut a release shortly after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants