TommyF
TommyF

Reputation: 7150

vue-test-utils & jest: How To Test Single File Component Filter?

I have a Single File Component with a filter defined

filters: {
  capitalize: function (value) {
    if (!value) return ''
    value = value.toString()
    return value.charAt(0).toUpperCase() + value.slice(1)
  }
},

I thought I could simply test it like

it('should capitalize input', () => {
  const wrapper = shallowMount(MyComp)
  expect(wrapper.vm.capitalize('foo')).toEqual('Foo')
  // or
  expect(wrapper.vm.filters.capitalize('foo')).toEqual('Foo')
})

but apparently I can't access a filter function like that directly and for some reason I cannot find in the docs how to do it correctly?

Upvotes: 2

Views: 725

Answers (1)

Flame
Flame

Reputation: 7618

You can access Vue filters using:

Vue.$options.filters.capitalize()

Upvotes: 3

Related Questions