-
Notifications
You must be signed in to change notification settings - Fork 668
Function properties in stubs should not render source code #975
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
Comments
Actually I'm not sure if this counts as a "bug" or rather a feature request... |
Here a workaround to ensure deterministic snapshots. const HTML_ATTRS = /(\S+)\s*=\s*([']|["])\s*([\W\w]*?)\s*\2/gim;
const DEFAULT_OPTIONS = {
functionRemplacement: '{[Function]}',
};
/**
* Function is a predicate, to test attribute.
* @callback AttrIgnoreFunction
* @param {string} name The attribute name
* @param {string} value The attribute name
* @return {Boolean} Return `true` to ignore the element, `false` otherwise.
*/
/**
* Serialize wrapper.
*
* `functionRemplacement` is used to ensure a deterministic snapshot (workaround of https://github.com/vuejs/vue-test-utils/issues/975)
*
* @param {Wrapper} wrapper
* @param {Object} options
* @param {string} options.functionRemplacement Default remplacement is '{[Function]}'.
* @param {AttrIgnoreFunction} options.attrIgnore Default is `undefined`.
*
*/
export function html(wrapper, options) {
const opts = {
...DEFAULT_OPTIONS,
...options,
};
return wrapper.html().replace(HTML_ATTRS, (matches, name, quote, value) => {
if (opts.functionRemplacement && /^\s*function\s*/gi.test(value)) {
return `${name}="${opts.functionRemplacement}"`;
}
if (opts.attrIgnore && opts.attrIgnore(name, value)) {
return '';
}
return matches;
});
} |
Is this issue on the roadmap? |
@briwa Can you reopen this issue? Since the PR that was going to fix it is closed. |
Do you have an estimate when it this going to be fixed? |
I couldn't get this to work, nothing changes. My error shows there are arrow functions, so I guess that's why. Any ideas on when this will be fixed? |
As a workaround, you can put So for your example it would be: <template>
<child-component :clickHandler="handleClick"/>
</template>
<script>
import ChildComponent from './ChildComponent';
export default {
name: 'test-component',
components: {
ChildComponent,
},
methods: {
/* istanbul ignore next */
handleClick() {
console.log('Click');
},
},
};
</script> The downside is, that the function will not be included for the test coverage. |
My workaround is to just ignore the error in the coverage report and not to run coverage on the CI. It's not a perfect solution but it suffices. |
Any news about this fix? The issue is occurring, in our case, while we are testing a component which doesn't pass a prop to the child component being defined as a Function type with default value in that specific child component as follows: The output of the jest coverage is the following: Thank you! |
Version
1.0.0-beta.25
Reproduction link
https://codesandbox.io/s/135my5kk9j
Steps to reproduce
What is expected?
wrapper.html() should return something like
What is actually happening?
I ran into the bug because the snapshot tests failed on our CI environment, even though I had properly updates the snapshots locally. The failing spot was a Function property which was rendered differently on local and CI environments (due to Istanbuls coverage comments being added).
The text was updated successfully, but these errors were encountered: