18

I am using Jest with Vue-Test-Utils. The code I have been using looks like this:

beforeEach(() => {
  wrapper = shallow(GridContainer, {
    data: {
      pageSize: count
    },
    propsData: {
      userId,
      managerId
    }
  })
})

In this example, I want to set the pageSize value before the life cycle mounted is called. The problem with the above code is that I have started getting the following warning when the tests run:

[Vue warn]: Do not use built-in or reserved HTML elements as component id: data

When I delete the data property above, the warning goes away.

Am I setting the data correctly? If so, what should I do about the warning?

Should I set the data another way?

Daryn
  • 3,394
  • 5
  • 30
  • 41

3 Answers3

26

Please try like this:

beforeEach(() => {
  wrapper = shallow(GridContainer, {
    propsData: {
     userId,
     managerId
    }
  })
 wrapper.setData({pageSize: count})
})

reference: setData https://vue-test-utils.vuejs.org/en/api/wrapper/setData.html

yukihirop
  • 502
  • 5
  • 6
17

The solution for me was to define the data as a function within the initialisation:

beforeEach(() => {
  wrapper = shallow(GridContainer, {
    propsData: {
      userId,
      managerId
    },
    data: function() {
      return {
        pageSize: count
      }
    }
  })
})

In my case, the solution provided by yukihirop using setData() did not work.

My component was accessing data from the window object which was undefined within my test environment. This data was being output within the template. This caused an exception to be thrown immediately after mounting - before I had a chance to call setData().

Nicholas Betsworth
  • 1,707
  • 19
  • 24
1

The solution of Nicolas Betsworth is correct, however, it is also possible to use setData with Vue.nextTick(). Vue.nextTick() will rerender the component.

beforeEach(() => {
  wrapper = shallow(GridContainer, {
    propsData: {
     userId,
     managerId
    }
  })
 wrapper.setData({pageSize: count})
 Vue.nextTick()
})

Vue.nextTick

tony19
  • 125,647
  • 18
  • 229
  • 307
mnovabox
  • 41
  • 5
  • If you are using async, you can await the `wrapper.setData`, and then you don't need the `nextTick()`. – Nick M Oct 27 '22 at 02:21