-
Notifications
You must be signed in to change notification settings - Fork 257
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
Props of component stubs are not reactive #362
Comments
Seems like a bug... I wonder why the stubbed components behave differently. As far as I can tell we pass the props to them in the same manner as a regular component - that is located here. |
One thing I forgot to mention in case that was not obvious: const innerComponent = wrapper.getComponent(InnerComponent); after the button click both tests are green. So the stubs only present the state of their props at the moment there are obtained from the wrapper. |
Oh, I see. I think it's likely best to get a new copy of the stub each time, anyway - this sounds like a practice we should be encouraging. It would be nice to investigate how we can fix this, I sort of suspect |
Yeah it might be something like that. @UrsMetz would you want to try to fix it and open a PR? Starting by adding a reproduction just like the one in your issue, and maybe trying to figure out what's going on here. I suspect there is something in https://github.com/vuejs/vue-test-utils-next/blob/master/src/utils/find.ts (maybe the array spreading?) that causes this. Let us know if you'd like to give it a try! |
@cexbrayat I think I will find some time this weekend to have a look at it :-). |
It would be useful for sure. We should do this. I mean, at least we have a work around for now. |
I had a look at it: if (stub === true || shallow) {
const propsDeclaration = type?.props || {}
const newStub = createStub({ name, propsDeclaration, props })
stubs[newStub.name] = newStub
return [
newStub,
props,
children,
patchFlag,
dynamicProps
]
} The parameter edit 2021-02-21: Fixed two typos in the last paragraph. |
That solution seems like it might lead to problems leaking cached stubs between tests (maybe) or lead to other unexpected side-effects. I am not clear on why Can you try adding a reproduction test, then adding your fix, and see if it breaks anything? Test Utils has a lot of hacks since we are doing things your normally cannot (and should not) do, so I don't worry too much if it feels a bit bad - as long as we have tests and comments explaining why things are the way they are, I think it's fine. I'd definitely recommend adding a link to this issue in both the test(s) you add and whatever else you modify inside of There might be some benefit in using a |
@lmiller1990 I think during my debugging |
Thanks for the reproduction test! If you want to go ahead and make a PR with your fix and test, we can review it and see if there is any obvious problems. If not, we can just go with it (if a bug does emerge in the future, we can deal with it then ... you don't know what you don't know). |
I hope I will find the time to prepare a PR this week (properly on the weekend) and will come back to you. |
I've created the PR #453 to address this issue. |
Nice job - the tests look thorough. I can review this soon. Thanks for the work! |
Merged #453 |
Subject of the issue
When stubbing inner components in a test (e.g. using shallow mounting) the
props
of the stubs are not reactive (where as theprops
of a non-stub inner component are).One example is a simple component with that passes data to an inner component and that data changes e.g. because a button is clicked, cf. example below.
Steps to reproduce
The following unit test (using jest and Vue 3) shows the difference when using stubs and real components:
edit:
Moving the line
after the button click makes both test run green.
end of edit
I can also provide a repo with the whole setup but it is basically a Vue 3 app created using the CLI (version 4.5.11).
Expected behaviour
The props of the stubbed component should also be updated so that the expectation is true and both tests succeed.
Actual behaviour
One the non-shallow test case succeeds the shallow one runs red.
Possible Solution
I'd say this is a regression because with Vue 2 and "old" vue-test-utils both test would run green. So the reactive behavior of props of stubs should be resemble that of real inner components.
The text was updated successfully, but these errors were encountered: