I have a component that is using an element-ui component.
Tree.vue
<div>
<el-tree :data="treeData" :props="config"></el-tree>
</div>
I want to test the interactivity with the component (for example, to be able to click on one of the el-tree
html elements), but when I'm using vue-test-utils
mount
to mount the Tree
component, it doesn't render it's children component, like it shallow
ing the component instead.
Tree.test.js
import Vue from 'vue';
import { mount } from 'vue-test-utils';
import Tree from './Tree.vue';
const wrapper = mount(Tree);
it('renders element ui tree component', () => {
console.log(wrapper.html());
});
Outputs:
<div><el-tree data="[object Object],[object Object]" props="[object Object]"></el-tree></div>
Any idea how can I fully render the Tree
component, including it's children?
We recommend using @testing-library/vue for testing components in applications, as its focus aligns better with the testing priorities of applications. Use @vue/test-utils only if you are building advanced components that require testing Vue-specific internals.
UI Toolkit. "Element is a Vue 2.0 based component library for developers, designers and product managers. It's built upon the principles of consistency, tangible feedback, efficiency and controllability."
Shallow mounting Like mount, it creates a Wrapper that contains the mounted and rendered Vue component, but with stubbed child components. Notice that using shallowMount will make the component under testing different from the component you run in your application – some of its parts won't be rendered!
I had a similar problem where element ui components were not recognized.
Had to import createLocalVue and ElementUI in the test file
import { shallow, mount, createLocalVue } from '@vue/test-utils'
import Vue from 'vue';
import ElementUI from 'element-ui';
import Editor from '../src/components/layout/Editor'
and then
const localVue = createLocalVue();
localVue.use(ElementUI);
and then
describe('Editor', () => {
let wrapper
beforeEach(() => {
wrapper = shallow(Editor, {localVue}, {
data : {
x: '0',
y: '0',
}
})
})
it('renders a vue instance', () => {
expect(wrapper.isVueInstance()).toBe(true);
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With