Sub topic 14
Measure coverage and debug failing tests
This chapter assumes you have a Salesforce DX project with @salesforce/sfdx-lwc-jest installed, and that you can write a basic Jest test for a Lightning web component. Here you will measure which lines your tests run, and you will learn how to track down a failing test.
Run a coverage report
Coverage shows which lines, branches and functions your tests execute. The test:unit:coverage script from the setup chapter passes --coverage to Jest:
{
"scripts": {
"test:unit": "sfdx-lwc-jest",
"test:unit:watch": "sfdx-lwc-jest -- --watch",
"test:unit:coverage": "sfdx-lwc-jest -- --coverage"
}
}Run it with npm run test:unit:coverage. Jest prints a table with one row per file and writes an HTML report to the coverage/ folder. Open coverage/lcov-report/index.html in a browser to see each source file with the unexecuted lines highlighted.
| Column | Meaning |
|---|---|
| % Stmts | Share of statements that ran |
| % Branch | Share of `if`, ternary and `&&` paths taken |
| % Funcs | Share of functions that were called |
| % Lines | Share of lines that ran |
| Uncovered Line #s | Line numbers that never ran |
Branch coverage is usually the lowest number. A handler with an if and no else needs two tests, one where the condition is true and one where it is false, to reach 100% branches.
Read the gaps
Take the counter component from earlier chapters. If its test never clicks the button, the report lists handleClick as uncovered. Add a test that clicks it and the gap closes. A missing branch is the more common case. Suppose you add a guard:
handleClick() {
if (this.step <= 0) {
return;
}
this.count += this.step;
}The existing test passes with step = 2, so the early return is never reached. The report shows the branch as uncovered. Write a second test that sets step to 0, clicks, and asserts that the count is unchanged.
Debug a failing test
Start by reading the failure message. Jest shows the expected value, the received value, and the line of the failed expect. Then narrow the problem down.
- Run one file:
npm run test:unit -- counter.test.js. - Run one test by name:
npm run test:unit -- -t "adds the step". You can also changeittoit.onlytemporarily. - Print the rendered output with
console.log(element.shadowRoot.innerHTML)right before the failing assertion. This shows what the component actually rendered. - Check that you waited for re-rendering with
await Promise.resolve(), or for pending promises after an Apex call. - Check that each test removes its component in
afterEach. Leftover elements indocument.bodycan make later tests fail.
Some failures come up often:
Cannot read properties of nullusually meansquerySelectorfound nothing. Print theinnerHTMLand check the selector.- An assertion sees the old text. The test asserted before the DOM re-rendered.
- An Apex mock returns
undefined. The mock was cleared or never given a resolved value in that test.
To step through code, run Jest under the Node inspector. Add this script to package.json:
{
"scripts": {
"test:unit:debug": "node --inspect-brk node_modules/.bin/sfdx-lwc-jest -- --runInBand"
}
}Run npm run test:unit:debug, then attach VS Code or open chrome://inspect in Chrome. Execution pauses on the first line, and your debugger; statements work. The --runInBand option runs tests in a single process, which the debugger needs.
Exercise
A test for a component with if (this.items.length === 0) { this.message = 'Empty'; } passes, but the coverage report shows 100% statements and 50% branches for that file. What is missing, and how do you debug a different test that asserts old text after a click?
Show answer
The test only covers one side of the if. Add a test where items is non-empty, so the condition is false and the branch is taken. For the stale text, the assertion most likely ran before re-rendering finished. Add await Promise.resolve() after the click, and print element.shadowRoot.innerHTML to confirm what was rendered.