4

I am testing a component which dynamically imports the child components. Here a screenshot of that section

Inside Parent.vue

Inside Parent.vue

Error

enter image description here

It works fine for normal imports but not with dynamic async import. Can anyone help on how configure Jest to support async component imports?

0

2 Answers 2

5

If problem is still relevant (I didn't found anydirect answer) try to 'stub' components which should be imported.

Component

<template>
    <div>
        <dynamic-imported-component-one id="componentOne"></dynamic-imported-component-one>
        <dynamic-imported-component-two id="componentTwo"></dynamic-imported-component-two>
    </div>
</template>
<script>
    const ComponentOne = resolve => import(/* webpackChunkName: "views/view-ComponentOne-vue" */ '../Components/ComponentOne.vue');
    const ComponentTwo = resolve => import(/* webpackChunkName: "views/view-ComponentTwo-vue" */ '../Components/ComponentTwo.vue');

    export default {
        components: {
            'dynamicImportedComponentOne': ComponentOne,
            'dynamicImportedComponentTwo': ComponentTwo }
    }
</script>

Test:

describe('SomeComponent.vue', () => {

    const stubs = {
        dynamicImportedComponentOne: '<h3>Stubbed component one</h3>',
        dynamicImportedComponentTwo: '<h3>Stubbed component one</h3>'
    }

    it('test for SomeComponent', () => {
        const wrapper = shallowMount(SomeComponent, { stubs });

        expect(wrapper.find('#componentOne').exists()).toBeTruthy();
        expect(wrapper.find('#componentTwo').exists()).toBeTruthy();
    });
});

You can't test the ComponentOne/ComponentTwo content, but it should be anyway done in separate tests.

Sign up to request clarification or add additional context in comments.

2 Comments

shallowMount automatically stubs all child component, I'm not sure if this approach helps in any way. I'm still getting the same error after trying to shallowMount and explicitly add the stubs.
It is correct that shallowMount should automatically stub all childs, but adding a specific stub helps.
0

Did you try using "dynamic-import-node" in you .babelrc ? It seemed to fix dynamic import of child components in a mounted parent for me. I got the pointer from https://github.com/enzymejs/enzyme/issues/1460#issuecomment-355193587 enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.