Skip to content

Testing component with props validation #4370

@ChangJoo-Park

Description

@ChangJoo-Park

Vue.js version

2.1.4

Reproduction Link

Steps to reproduce

Component A

<template lang="pug">
  div.header
    h2.header--date {{nowDate}}
    h3.header--time {{nowTime}}
</template>
<script>
export default {
  props: {
    nowDate: {
      type: String,
      required: true
    },
    nowTime: {
      type: String,
      required: true
    }
  }
}
</script>

Test

  it('should date and time are string', () => {
    const vm = getViewModel(AppHeader, {nowDate: 1, nowTime: 1})
  })

Helper Method

function getViewModel (Component, propsData) {
  const Ctor = Vue.extend(Component)
  const vm = new Ctor({ propsData }).$mount()
  return vm
}
ERROR LOG: '[Vue warn]: Invalid prop: type check failed for prop "nowDate". Expected String, got Number.
(found in root instance)'
ERROR LOG: '[Vue warn]: Invalid prop: type check failed for prop "nowTime". Expected String, got Number.
(found in root instance)

How to test invalid prop type?

What is Expected?

expect(vm).to.throws()

I expected test passed but AssertionError: expected { Object (_uid, _isVue, ...) } to be a function

What is actually happening?

pass test

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions