[Vuejs]-Vue jest test activated lifecycle

3👍

activated life cycle hook triggers only on keep-alive components.
https://v2.vuejs.org/v2/api/#activated

For non keep-alive components use mounted instead.

If keep-alive component, have in mind that is not called during server-side rendering. If not the case, provide additional info

3👍

You can call the activated() hook this way:

wrapper.vm.$options.activated[0].call(wrapper.vm)

Looks hacky, but I haven’t found any other way.

  • wrapper.vm.$options.activated is an array, so you need to access the first element
  • then you have to use call(wrapper.vm), to set the context (this variable), so it’s called on your component and it can access its methods, data, etc.

This solution is valid for @vue/test-utils 1.1.1. I wouldn’t consider it particularly safe and reliable, because it does not use the public API, so at some point it may stop working.
It’s still better than nothing though.

👤yjohn

Leave a comment