最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Missing required prop error in Vue unit test - Stack Overflow

programmeradmin9浏览0评论

I have a ponent with two required props. The unit test is failing because I get the following errors:

  console.error node_modules\vue\dist\vue.runtimemon.js:589
    [Vue warn]: Missing required prop: "heading"

    (found in <Root>)

  console.error node_modules\vue\dist\vue.runtimemon.js:589
    [Vue warn]: Missing required prop: "subHeading"

    (found in <Root>)

  console.error node_modules\vue\dist\vue.runtimemon.js:589
    [Vue warn]: Missing required prop: "heading"

    (found in <Root>)

  console.error node_modules\vue\dist\vue.runtimemon.js:589
    [Vue warn]: Missing required prop: "subHeading"

Component source:

<template>
    <div class="level-item has-text-centered">
        <div>
            <p class="heading">{{ heading }}</p>
            <p class="title">{{ subHeading }}</p>
        </div>
    </div>
</template>

<script>
export default {
    name: 'StatisticsBannerItem',
    props: {
        heading: {
            required: true
        },
        subHeading: {
            required: true
        }
    }
};
</script>

Test source:

import { shallow, mount } from '@vue/test-utils';
import StatisticsBannerItem from '../../../src/ponents/statistics-banner-item.vue';


describe('Statistics Banner', () => {  
    it('renders item with props data correctly', () => {
        const wrapper = shallow(StatisticsBannerItem, {
            propsData: {
                heading: 'Test Heading',
                subHeading: 'Test Subheading'
            }
        });
        // assertions
    });
});

However, according to the official documentation, the propsData object should be providing the props to the ponent.

How do I correctly pass props to a ponent in a test?

I have a ponent with two required props. The unit test is failing because I get the following errors:

  console.error node_modules\vue\dist\vue.runtime.mon.js:589
    [Vue warn]: Missing required prop: "heading"

    (found in <Root>)

  console.error node_modules\vue\dist\vue.runtime.mon.js:589
    [Vue warn]: Missing required prop: "subHeading"

    (found in <Root>)

  console.error node_modules\vue\dist\vue.runtime.mon.js:589
    [Vue warn]: Missing required prop: "heading"

    (found in <Root>)

  console.error node_modules\vue\dist\vue.runtime.mon.js:589
    [Vue warn]: Missing required prop: "subHeading"

Component source:

<template>
    <div class="level-item has-text-centered">
        <div>
            <p class="heading">{{ heading }}</p>
            <p class="title">{{ subHeading }}</p>
        </div>
    </div>
</template>

<script>
export default {
    name: 'StatisticsBannerItem',
    props: {
        heading: {
            required: true
        },
        subHeading: {
            required: true
        }
    }
};
</script>

Test source:

import { shallow, mount } from '@vue/test-utils';
import StatisticsBannerItem from '../../../src/ponents/statistics-banner-item.vue';


describe('Statistics Banner', () => {  
    it('renders item with props data correctly', () => {
        const wrapper = shallow(StatisticsBannerItem, {
            propsData: {
                heading: 'Test Heading',
                subHeading: 'Test Subheading'
            }
        });
        // assertions
    });
});

However, according to the official documentation, the propsData object should be providing the props to the ponent.

How do I correctly pass props to a ponent in a test?

Share Improve this question asked Apr 14, 2018 at 18:51 user9993user9993 6,18012 gold badges59 silver badges123 bronze badges 6
  • The code you shared is correct, so maybe the problem es from another test or another ponent which uses this ponent. – Emile Bergeron Commented Apr 14, 2018 at 19:00
  • I have isolated this single test, nothing else is using it. – user9993 Commented Apr 14, 2018 at 19:03
  • Is it a typo in the real code you're using? like propsData: { propsData: {}} or something like that? – Emile Bergeron Commented Apr 14, 2018 at 19:22
  • 1 Your code is fine. Just tested it. You have some other problem, somewhere else. – acdcjunior Commented Apr 14, 2018 at 19:40
  • That is unfortunate, guess I'm stuck then. – user9993 Commented Apr 14, 2018 at 20:00
 |  Show 1 more ment

1 Answer 1

Reset to default 6

The problem was the test output was not as clear as it perhaps should be. The errors were ing from another test (another it inside the same file) that was not being passed props - the test output was arranged in a way that made it appear as though the errors were ing from the test in the question.

发布评论

评论列表(0)

  1. 暂无评论