Vue-test-utils: Mocking a Method for the 'created' Lifecycle Hook

Created on 17 Nov 2017  路  3Comments  路  Source: vuejs/vue-test-utils

I'd like to stub/spy a method and ensure/test that it is called during the created lifecycle hook. I've discovered that I can use wrapper.setMethods to stub/mock the method, but the created hook has already run at this point; I've tried using wrapper.update(), but it doesn't appear to run the created hook again.

Am I missing something simple?

Most helpful comment

You can pass a methods object in the mounting options that will overwrite any method inside the component.

Say you had a method called methodToStub in your component. You would mount like this:

mount(Component, {
  methods: {
    methodToStub: () => {}
  }
})

All 3 comments

You can pass a methods object in the mounting options that will overwrite any method inside the component.

Say you had a method called methodToStub in your component. You would mount like this:

mount(Component, {
  methods: {
    methodToStub: () => {}
  }
})

See the issue #166 and the pending PR #167 that would solve this issue.

Thanks, @eddyerburgh! Passing methods to mount/shallow was exactly what I needed.

Was this page helpful?
0 / 5 - 0 ratings