Sub topic 1
Why test Lightning web components with Jest
A Lightning web component (LWC) is a JavaScript module with a template, and it runs in the browser. You can check it by deploying it to an org and clicking through the page. That works for one change. It becomes slow and unreliable once you have dozens of components that other people also edit. This chapter explains what Jest gives you instead and where it fits among your other checks.
Prerequisites
This chapter assumes you already know the following:
- The basics of Lightning web components: templates,
@apiproperties, and event handling. - Modern JavaScript, including promises and
async/await. - How to use Node.js and npm from a terminal.
- How a Salesforce DX project is laid out, at a basic level.
What a Jest unit test does
Jest is a JavaScript test runner. It finds test files, runs them in Node.js, and reports which assertions passed or failed. For LWC, Salesforce supplies a package called @salesforce/sfdx-lwc-jest. It configures Jest so that import ... from 'lwc' works and so that Salesforce-specific imports can be replaced with stand-ins.
Each test creates one component in a simulated browser document, which is a DOM implementation called jsdom. The test then changes inputs, such as a property or a click. Finally it checks the outputs: the rendered DOM, the events fired, and the calls made to mocked dependencies.
The component is tested in isolation. No org, no login, and no network are involved.
Why it pays off
- Speed. A suite of hundreds of tests typically runs in seconds. A deploy-and-click loop takes minutes.
- Repeatability. Tests do not depend on org data, permissions, or the state of a sandbox. The same input gives the same result on every machine.
- Edge cases. An Apex error, an empty list, and a slow response are hard to trigger in an org. In a test you can produce each of them with one line of mock setup.
- Safe refactoring. When you rename a property or restructure a template, failing tests show exactly what changed.
- Continuous integration. Jest runs from the command line, so a pipeline can block a merge when tests fail.
What Jest does not cover
A unit test checks one component against mocked surroundings. It cannot tell you that the real Apex method returns the fields your component expects. It also cannot tell you that a page layout places your component correctly or that sharing rules allow a user to see the data.
The diagram shows where each kind of check sits.
How the rest of the tutorial proceeds
The next chapters follow the order in which you will write tests:
- Install the tooling and run a first test.
- Learn the structure of a Jest test file.
- Render a component with
createElementand query its shadow DOM. - Wait for asynchronous re-rendering before asserting.
- Test properties, events, and mocked Apex calls.
- Test wired components, base components, and platform modules.
- Measure coverage and debug failures.
Every chapter works on a small component, so you can run each example as you read.
Exercise
A component loads contacts with an Apex method. Name one thing a Jest unit test can verify about it, and one thing it cannot.
Show answer
A Jest test can verify that the component renders the contact names returned by a mocked Apex method, or that it shows an error message when the mock rejects. It cannot verify that the real Apex method returns the right records or respects sharing rules, because the Apex call is replaced by a mock and no org is involved.