Search code examples
javascriptunit-testingvue.jsjestjsvue-test-utils

Vue Testing - '... .push is not a function'


Im trying to test a method of a component that adds a new entry to an already existing object.

addTag: function () {
  this.value[this.field.key].push(this.tag)
  this.tag = ''
}

I am just trying to call that method inside my test via

    wrapper.setProps({
      field: {
        key: 'tag'
      },
      value: {
        tag: {}
      }
    })
...
    wrapper.vm.addTag()

but it throws and error

TypeError: this.value[this.field.key].push is not a function

Ive set all needed data and props beforehand (field.key and tag), so that's not the problem. running other methods works completly fine, push seems to be the problem


Solution

  • This is because this.value['tag'] is an object, not an array, so there is no push method.

    Defining it as an array instead would change that:

    wrapper.setProps({
      field: {
        key: 'tag'
      },
      value: {
        tag: []
      }
    })