Skip to content
StackDecode
AdvancedLesson 26 of 37

LWC lifecycle and testing

Hooks, debugging and Jest.

Lifecycle: constructor → connectedCallback (inserted in the DOM; fetch data) → render → renderedCallback (after every render; avoid infinite loops) → disconnectedCallback (cleanup).

Test with Jest using @salesforce/sfdx-lwc-jest:

javascript
import { createElement } from 'lwc';
import Hello from 'c/hello';

it('renders the name', () => {
  const el = createElement('c-hello', { is: Hello });
  el.name = 'Ada';
  document.body.appendChild(el);
  expect(el.shadowRoot.textContent).toContain('Ada');
});

Use browser dev tools and Lightning Debug Mode while developing.