Skip to content
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

Testing component with props validation #4370

Closed
ChangJoo-Park opened this issue Dec 2, 2016 · 2 comments
Closed

Testing component with props validation #4370

ChangJoo-Park opened this issue Dec 2, 2016 · 2 comments

Comments

@ChangJoo-Park
Copy link

ChangJoo-Park commented Dec 2, 2016

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

@yyx990803
Copy link
Member

Vue warnings are not thrown errors. You can spy on console.error to assert Vue warnings.

@matrunchyk
Copy link

matrunchyk commented Mar 22, 2017

I managed to achieve the spying on console.error as @yyx990803 suggested:

it('fails if no prop passed', () => {
        const errors = [];
        const oldError = console.error;  // eslint-disable-line

        console.error = error => errors.push(error); // eslint-disable-line
        getViewModel(YourComponentName);
        // Copy&Paste a Vue warning below
        expect(
            errors.find(error => error.indexOf('Missing required prop: "visible"') > -1)
        ).to.not.equal(undefined);
        console.error = oldError;  // eslint-disable-line
    });

Or you can use sinon.spy(console, "log");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants