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.

Add API to trigger lifeCycles manually.

See original GitHub issue

Feature Description

Some lifecycles are hard to be test in certain cases. For example, I have no idea on how to test activated in shallowMount mode, since <keep-alive> will be a stub. Howerver shallowMount is a MUST in my complicated applications.

Expected behaviour

provide an API like:

// does exacly what it says
// other lifeCycles should work, too.
wrapper.triggerLifeCycle('activated') 

my solution for now (which works):

export function triggerLifeCycle(vm, lifeCycle) {
  if (vm.$options[lifeCycle]) {
    vm.$options[lifeCycle].forEach(fn => {
      fn.call(vm);
    });
  }
}

if the idea sounds accepatble, I’m glad to make a PR. thanks.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wxsmscommented, Nov 11, 2021

@bponomarenko Thank you very much for the kind answer and solution. You do solved my problem in a way that I have not consider yet. And I kind of agree your point about testing indeed. Good day.

1reaction
bponomarenkocommented, Nov 11, 2021

But I think that white box testing is also useful in some cases.

I have been working with Vue codebases for around 3-4 years, and in my experience such “granular” unit tests usually hard to maintain and they break to0 often without tangible reasons. Fixing those “false positives” takes more time than the actual value they bring.

But let me try to address your questions:

  1. This is interesting challenge 🤔 But I think you don’t even need Vue test utils to test that:
import Vue from 'vue';
import TestComponent from 'src/components/test-component';

it('test', async () => {
  // Create component constructor
  const Comp = Vue.extend(TestComponent);
  const fetchDataSpy = jest.spyOn(Comp.options.methods, 'fetchData');
  
  expect(fetchDataSpy).not.toHaveBeenCalled();
  
  // Trigger component creation
  const instance = new Comp();
  
  expect(fetchDataSpy).toHaveBeenCalledTimes(1);
  
  // Explicitly mount component
  instance.$mount();
  
  expect(fetchDataSpy).toHaveBeenCalledTimes(2);
})
  1. I guess example from the previous answer with slightly different checks should cover this used case too:
// ...

// Trigger component creation
const instance = new Comp();
  
expect(fetchDataSpy).not.toHaveBeenCalled();
  
// Explicitly mount component
instance.$mount();
  
expect(fetchDataSpy).toHaveBeenCalledTimes(1);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting lifecycle configuration on a bucket - AWS Documentation
This section explains how you can set a S3 Lifecycle configuration on a bucket using AWS SDKs, the AWS CLI, or the Amazon...
Read more >
How to trigger a Lifecycle event - ServiceNow Community
Triggering a lifecycle event in ServiceNow can be done either automatically or manually. This blog post will lay out the steps for both....
Read more >
Page Lifecycle API - Is it possible to manually freeze a page
I have simple event listeners to run a console.log when the either freeze or resume events are triggered. Does anybody know a way...
Read more >
Create or update lifecycle policy API | Elasticsearch Guide [8.5]
The following example creates a new policy named my_policy . In addition, you can use the _meta parameter to add arbitrary metadata to...
Read more >
Get or set lifecycle configuration for a bucket | Cloud Storage
Sets the lifecycle management configuration on one or more buckets. The config-json-file specified on the command line should be a path to a...
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