-
Notifications
You must be signed in to change notification settings - Fork 668
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
"Avoid mutating a prop directly" warning when updating prop #631
Comments
Thanks for the bug report. This is caused by a recent change to handling asynchronous updates. This should be fixed in the coming weeks. |
@eddyerburgh Thanks |
Just wondering if it makes sense to extend the configuration of test-utils and enable/disable the warnings. It might control the Vue.config.silent when creating the instance. Would it be a correct solution? |
Yes I think that's the only solution at the moment. We would need to make sure no important warnings are hidden. Also we would need to set the config to silent in all methods that interact with the instance, like |
Does that mean Vue is complaining about a non-issue here, or just that you want the option to get rid of pesky logging? :) |
Sort of. The problem is how we implemented synchronous updates, you can read an explanation here—#676. |
This issue was fixed in beta.19 |
@eddyerburgh I'm still seeing this happening when using
<script>
export default {
name: 'Something',
props: {
value: {
required: true,
type: String,
},
},
};
</script>
<template>
<div>{{ value }}</div>
</template>
import Vue from 'vue';
import Something from './something.vue';
import { shallowMount } from '@vue/test-utils';
describe('Something', () => {
it('warns', done => {
Vue.config.warnHandler = done.fail;
const wrapper = shallowMount(Something, {
propsData: {
value: 'first',
},
});
wrapper.setProps({
value: 'second',
});
done();
});
}); leads to:
Is there a way to avoid this? |
Looking at the fix in #688, the above makes sense. Is there a way to get warnings from application code but not from Vue test utils? 🤔 |
Current workaround: Vue.config.warnHandler = (msg, vm, trace) => {
const currentStack = new Error().stack;
const isInVueTestUtils = currentStack.split('\n').some(line => line.startsWith(' at VueWrapper.setProps ('));
if (isInVueTestUtils) {
return;
}
done.fail(`${msg}${trace}`);
}; |
Workaround in a mocha test-setup: function fixWarnHandler() {
Vue.config.warnHandler = (msg, vm, trace) => {
if (msg.includes('Prop being mutated:')) return; // False warning: abort.
console.log(`${msg}${trace}`); // Real warnings pass through.
};
}
afterEach (() => { Vue.config.warnHandler = undefined }); // Reset every time, just in case.
it('test', () => {
fixWarnHandler(); // <--- Add only when invalid warning is anticipated.
foo.should.equal(bar);
}); |
hey @gitlab-winnie thanks for reporting this. I am not sure whether relying on warnHandler would be the right thing. I am wondering if test-utils would be helpful decorating it, but it would be triggered anyway. |
@cdbkr I'm not sure if I described the problem I have clear enough, so let me rephrase it: We want to fail our test suite for any Vue warning but of course not for correct use of Vue test utils. Would it be possible to introduce a smarter way of #631 (comment) in Vue test utils? That is, catch all warnings from Vue test utils but deliver the rest to the configured warn handler. |
ok, I see your point now |
So this is still a bug, I'm encountering it as well in a totally non-prop-updating situation :) |
@vperron I think #1062 will fix it. @eddyerburgh Can you confirm? |
Yes I believe so @gitlab-winnie. I can't confirm without a reproduction, but there are many subtle issues that should be solved by that PR. |
What exactly does #1062 fix? I'm still getting the error with stuff like: it('whatever', () => {
component.vm.someProp = 'a';
expect(component.vm.someProp).toBe('a');
}
) |
I think it would be useful to also make This could then be Then I could simply do Would this be possible? I tried
to no avail. |
I recommend not doing this. The "fix" for this issue just silences all warnings, which sucks. |
Version
1.0.0-beta.16
Reproduction link
https://codesandbox.io/s/vnv9wzp8rl
Steps to reproduce
See repro CodeSandbox.
What is expected?
Prop is updated and value changes without warnings
What is actually happening?
An warning is logged, complaining about changing a prop that isn't actually touched at all:
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "actions"
found in
---> at src/components/MobileCollapseRow.vue
Note that the test in de CodeSandbox example does succeed, but keep an eye on the console tab.
Note that the warning complains about another prop! Something fishy is going on here..
Note: Removing the child component (HelloWorld) from the base component (MobileCollapseRow) makes the warning disappear.
Note: Removing the
<a>
where the prop is used from the base component makes the warning disappear.The text was updated successfully, but these errors were encountered: