grzmol/vibe-force/sf-lwc-jest-testing
Claude Code and agent skill by grzmol/vibe-force
- community
- ext:[{"type":"hook","description":"Pre/post lifecycle hooks"}]
Checking that the SKILL.md file is still in the repository.
Covers unit testing Lightning Web Components with @salesforce/sfdx-lwc-jest - jest.config.js setup and the sfdx-lwc-jest preset, createElement/appendChild test anatomy, DOM cleanup, flushing async rerenders, querying shadow and light DOM, asserting dispatched CustomEvents, mocking Apex methods and @salesforce scoped imports with jest.mock, driving wire adapters with createApexTestWireAdapter/createLdsTestWireAdapter/createTestWireAdapter, stubbing lightning/navigation, lightning/messageService, toasts and base components, coverage configuration against the jestCoverageMin gate, and debugging failing suites. Use this skill whenever a __tests__ directory under force-app/**/lwc/** is created or changed, when vf-check jest fails, when a component test needs wire or Apex data, or when deciding what an LWC test should assert.
Install
npx vskill@latest install grzmol/vibe-force/sf-lwc-jest-testingSemi-extensibleMentions customization, but needs skill-specific knowledge. Read the extensible skills standard
SKILL.md
The same SKILL.md was published earlier as grzmol/vibe-force/sf-lwc-jest-testing.
LWC Jest Testing
When to use
| Situation | Use this skill | |
|---|---|---|
Adding or fixing a __tests__/*.test.js file for an LWC | Yes | |
vf-check jest fails or coverage is below the gate | Yes | |
| A component needs wire data, Apex results, or navigation asserted in a test | Yes | |
| Writing the component itself | Skill sf-lwc-development | |
Apex unit tests (@IsTest) | Skill sf-apex-testing | |
| Verifying behaviour in a real org after deploy | Skill sf-post-deploy-verification | |
Sharing one set of states between the spec and a sf lightning dev component preview | Skill sf-local-development, pattern 9 | |
Specs generated by DX MCP tools or the experience-lwc-test skill | Yes - review them against this skill; see sf-lwc-development/references/mcp-and-skills.md |
Jest runs entirely offline: no org, no authentication, no network. It is the cheapest gate in the pipeline and runs in wave 2 as part of vf-check local.
Quick reference
| Task | API | |
|---|---|---|
| Instantiate | const el = createElement('c-foo', { is: Foo }); document.body.appendChild(el); | |
| Set a public property | Assign before appendChild for initial render, after for a rerender | |
| Clean up | while (document.body.firstChild) document.body.removeChild(document.body.firstChild); in afterEach | |
| Wait for rerender | await Promise.resolve(); or a flushPromises() helper | |
| Query shadow DOM | el.shadowRoot.querySelector('.name'), querySelectorAll | |
| Query light DOM component | el.querySelector(...) - there is no shadowRoot | |
| Assert a dispatched event | el.addEventListener('rowselect', handler) then assert handler.mock.calls[0][0].detail | |
| Mock an Apex method | jest.mock('@salesforce/apex/Cls.method', () => ({ default: jest.fn() }), { virtual: true }) | |
| Emit wire data | adapter.emit(data) from a __mocks__ module built with createLdsTestWireAdapter | |
| Emit a wire error | adapter.error(body, status, statusText) or adapter.emitError({...}) | |
| Inspect the resolved wire config | adapter.getLastConfig() | |
| Control time | jest.useFakeTimers() + jest.advanceTimersByTime(ms) | |
| Run one file | npm run test:unit -- src/path/foo.test.js |
Core patterns
1. Project setup
sfdx-lwc-jest configures Jest for the DX workspace out of the box; jest.config.js is only needed to override. Stubs for every lightning namespace base component ship with the package and are used automatically. The coverageThreshold mirrors gates.jestCoverageMin (80) from .vibeforce/config.json; vf-check jest enforces the same number independently.
2. Test anatomy
Rules:
- The tag name passed to
createElementmust be the kebab-case name with thec-namespace. - Nothing renders until
document.body.appendChild(element). - Never share an element between
itblocks.
3. Asynchronous rerenders
One await Promise.resolve() flushes one microtask turn. Chained promises (an imperative Apex call that then triggers a rerender) need one flush per turn, or await Promise.resolve().then(() => Promise.resolve()). Never use setTimeout to "wait for" a rerender.
4. Mocking Apex
Assert both the success and the failure path for every server call - the error branch is where production bugs live.
5. Driving wire adapters
@salesforce/sfdx-lwc-jest re-exports the @salesforce/wire-service-jest-util APIs, so no extra dependency is required.
| Factory | Use for | Emitted shape | |
|---|---|---|---|
createLdsTestWireAdapter(fn) | getRecord, getObjectInfo, getPicklistValues, graphql | { data, error }, default error 404 NOT_FOUND | |
createApexTestWireAdapter(fn) | @wire(apexMethod) | { data, error }, default error 400 Bad Request | |
createTestWireAdapter(fn) | CurrentPageReference, MessageContext, custom adapters | Raw value, as emitted |
Error paths: getRecord.error() emits the default not-found response; getRecord.emitError({ body: { message: 'Boom' }, status: 500, statusText: 'Server Error' }) emits a specific one.
6. Asserting dispatched events
For toasts, listen for ShowToastEventName imported from lightning/platformShowToastEvent.
7. What to assert, and what not to
| Assert | Do not assert | |
|---|---|---|
| Rendered text, element counts, and attributes a user perceives | Internal field names or private method calls | |
| Which branch renders for a given input (loading / data / empty / error) | That a getter returns what the field holds | |
Event name, detail, and propagation flags | That dispatchEvent was called | |
| Apex/LDS call arguments (they are the cross-slice contract) | Base component internals - they are stubs | |
Accessibility attributes the template promises (alternative-text, aria-*) | CSS class strings with no behavioural meaning |
A test that would still pass after the component is broken is worse than no test. Snapshot tests are allowed only for stable presentational markup; a snapshot that is regenerated with -u every sprint is noise and must be deleted.
8. Timers and intervals
Anti-patterns
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/sf-lwc-jest-testing/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Extension points
hookPre/post lifecycle hooks
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/grzmol/vibe-force/sf-lwc-jest-testing)