sf-lwc-jest-testing

Writes and debugs Jest unit tests for Lightning Web Components using sfdx-lwc-jest.

2|Updated Sep 12, 2026
One-click install
npx skills add https://github.com/grzmol/vibe-force --skill sf-lwc-jest-testing-grzmol
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sf-lwc-jest-testing
Source: https://github.com/grzmol/vibe-force/tree/main/skills/sf-lwc-jest-testing
Command: npx skills add https://github.com/grzmol/vibe-force --skill sf-lwc-jest-testing-grzmol

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @salesforce/sfdx-lwc-jest, @salesforce/wire-service-jest-util, @sa11y/jest, jest-canvas-mock, and includes references (resource) components.

What problem does it solve? Lightning Web Component tests fail for subtle reasons: unflushed rerender microtasks, wire adapters that never provision, Apex mocks missing the virtual flag, and leaked jsdom state between tests. This Skill provides the exact patterns, mocks, and troubleshooting workflow to write LWC Jest tests that assert real behavior and pass the coverage gate. ## Core Features & Use Cases - Complete test patterns: Covers createElement/appendChild anatomy, DOM cleanup, async rerender flushing, shadow and light DOM querying, CustomEvent assertions, and fake timers. - Mocking cookbook: Provides ready-to-use mocks for imperative and wired Apex, lightning/uiRecordApi, navigation, messageService, toasts, labels, and base components using createApexTestWireAdapter and createLdsTestWireAdapter. - Gate integration and debugging: Aligns Jest coverage thresholds with the vf-check jest gate and supplies a symptom-to-cause troubleshooting table for failing suites. - Use Case: A developer adds a tests directory for a new accountCard component, uses the wire-driven recipe to emit getRecord fixture data, asserts the error branch, and verifies coverage passes the 80 percent gate before handoff. ## Quick Start Ask the assistant to write a Jest test for a Lightning Web Component that wires an Apex method, including success and error path assertions.

Frequently Asked Questions about sf-lwc-jest-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test a Lightning Web Component with Jest?

Use createElement from lwc to instantiate the component, append it to document.body, then assert against element.shadowRoot.querySelector results. Install @salesforce/sfdx-lwc-jest, which configures Jest for the DX workspace and ships stubs for all lightning base components.

How do I mock an Apex method in an LWC Jest test?

Declare jest.mock with the full @salesforce/apex/Class.method path, a factory returning { default: jest.fn() }, and the { virtual: true } option since the path is synthetic. For @wire usage, return createApexTestWireAdapter(jest.fn()) and emit data through the adapter.

Why does my LWC Jest test fail with an empty assertion result?

The most common cause is asserting before the rerender microtask completes; add await Promise.resolve() after setting properties or emitting wire data. Other causes are the wire config resolving to undefined so the adapter never fires, or missing DOM cleanup leaking state between tests.

Does sfdx-lwc-jest require a Salesforce org connection?

No, Jest runs entirely offline with no org, authentication, or network access. All server interaction is simulated through wire test adapters and mocked Apex functions, so org-dependent behavior must be verified separately with Apex tests or post-deploy checks.

How do I test lightning/navigation in an LWC Jest test?

Map lightning/navigation to a hand-written mock in moduleNameMapper that exposes a NavigationMixin with jest.fn() spies for Navigate and GenerateUrl. Then assert the captured pageReference type, attributes, and actionName after clicking the element that triggers navigation.

What should an LWC Jest test assert and not assert?

Assert rendered text, element counts, branch selection for loading/data/empty/error states, event names with detail payloads, and Apex call arguments. Do not assert private fields, internal method calls, base component stub internals, or snapshots that are regenerated every sprint.