question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

testing with lifecycle hooks (onMounted, ...)

See original GitHub issue

I tried to test my composition function which contains a lifecycle hook:

count.ts

import { onMounted, ref } from 'vue'

export default function withCount() {
  const count = ref(0)

  function increment() {
    count.value = count.value + 1
  }

  onMounted(() => {
    count.value = 23
  })

  return {
    count,
    increment
  }
}

count.spec.ts

import withCount from './count'

describe('counting', function() {

  it("should count", () => {
    const counter = withCount()

    expect(counter.count.value).toEqual(0)
    counter.increment()
    expect(counter.count.value).toEqual(1)
  })
})

Resulting in the following warning:

console.warn
    [Vue warn]: onMounted is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement.

       8 |   }
       9 | 
    > 10 |   onMounted(() => {
         |   ^
      11 |     count.value = 23
      12 |   })
      13 | 

Is it possible to get rid of the warning? Ideally it should be possible to trigger the lifecycle calls in the test.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ktsncommented, Jan 24, 2021

Lifecycle hooks have to depend on a vue component. Imagine you have onUpdated and onDestroy hooks. How do you test them without vue components?

If you want to just save types, you can use utils like vue-composable-tester

0reactions
MichaelSpcommented, Jan 5, 2021

@ygj6 You are right, but wouldn’t you agree that it’s much easier to test just a plain old function instead of a function with bells and whistles around? Especially the life-cycle hooks are not a relevant part of the testing environment. They should not even come from a vue-component because the component doesn’t have to exist and even if it existed (just for the purpose of wrapping the function) it would have artificially triggered life-cycle events. And there is also the argument of separation of concern embedded in the design of the Composition API that argues that you can split template and logic.

Let me propose the following API:

# count.ts
function useCounter() {
  const count = ref(0)

  function increment() {
    count.value++
  }

  onMounted(() => {
    increment()
  })

  return {
    count,
    increment,
  }
}

# count.spec.ts
it('setup', () => {
  const { fn, hooks } = setupForTest( () => useCounter(/* props */) )  // new function `setupForTest`
  expect(fn.count).toBe(0)
  hooks.onMounted()
  expect(fn.count).toBe(1)
  fn.increment()
  expect(fn.count).toBe(2)
})

Read more comments on GitHub >

github_iconTop Results From Across the Web

Learn How to Test Vue.js Lifecycle Methods
The lesson contains examples of how to test the mounted , beforeDestroy and destroyed hook. It should give you the insights to test...
Read more >
Testing Vue.js lifecycle hooks - Medium
The method init() was mocked with jest.fn() before the creation of the component, so that the mounted lifecycle hook was not yet passed...
Read more >
testing method calls in `mounted` lifecycle hook in vue test ...
I have the following lifecycle hook in my component ...
Read more >
Lifecycle Hooks | Vue.js
Each Vue component instance goes through a series of initialization steps when it's created - for example, it needs to set up data...
Read more >
Vue Lifecycle Hooks | Front-End Development Projects with ...
created : You will be able to access reactive data and events, but the templates and DOM are not mounted or rendered. This...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found