Sub topic 14
Testing and debugging site components
This chapter assumes you can write a basic Lightning web component, know Jest at a beginner level, and have a Salesforce DX project with the Salesforce CLI installed. It uses the CaseList component from earlier chapters.
Site components fail in ways that ordinary org pages do not. A guest user may lack access to a field. A property set in Experience Builder may arrive as a string. Two layers of testing cover most of this: Jest tests for logic, and manual checks in the live site as the right user.
Unit testing with Jest
Jest tests run on your machine without an org. The @salesforce/sfdx-lwc-jest package supplies stubs for Salesforce modules. Install it once:
npm install --save-dev @salesforce/sfdx-lwc-jestAdd a test:unit script that runs sfdx-lwc-jest, then put tests in a __tests__ folder next to the component.
The @salesforce/user/isGuest import is a good example. The stub returns false by default. To test the guest path, mock the module with jest.mock using the { virtual: true } option.
import { createElement } from 'lwc';
import CaseList from 'c/caseList';
import getMyCases from '@salesforce/apex/CaseListController.getMyCases';
jest.mock(
'@salesforce/user/isGuest',
() => ({ default: true }),
{ virtual: true }
);
describe('c-case-list as a guest', () => {
afterEach(() => {
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});
it('shows the login prompt', async () => {
const element = createElement('c-case-list', { is: CaseList });
document.body.appendChild(element);
await Promise.resolve();
const p = element.shadowRoot.querySelector('p');
expect(p.textContent).toBe('Log in to see your cases.');
});
});To test the signed-in path, use a second test file with isGuest set to false. jest.mock is hoisted, so you cannot change its value inside one file.
For wired Apex, use the createApexTestWireAdapter helper from @salesforce/sfdx-lwc-jest. Register the adapter, then call emit with sample records. Use emit with an empty array to cover the "No cases found." branch.
Debugging in the live site
Jest cannot show you what the platform does, so you also need to debug in a browser.
- Enable Lightning Web Security debug mode, or in an Aura site turn on Debug Mode for your user in Setup. This stops minification and gives readable stack traces.
- Open browser developer tools and use
console.logordebuggerstatements. Remove them before you deploy. - In Experience Builder, use Preview and the Guest or Experience user option where available. Better still, log in as a real test user of each profile.
- In Setup, open Debug Logs and trace the site user. Apex errors, such as a failed
WITH USER_MODEquery, appear there.
Common failures
| Symptom | Likely cause | Check |
|---|---|---|
| Component missing from the builder palette | `isExposed` is false or the target is wrong | Meta file targets |
| Empty list for guests only | No object, field, or sharing access | Guest profile and sharing rules |
| Property change has no effect | Property name differs from the `@api` name | Meta file and JS class |
| Changes not visible on the live site | Site not republished | Publish again |
Exercise
A teammate says the CaseList component works in Jest and in preview as an admin, but guests see "No cases found." Name two places to check.
Show answer
Check the guest user profile for read access to the Case object and fields, and check the sharing settings for guest records. A debug log for the guest site user also shows any query errors from WITH USER_MODE.
Exercise
Why can't you switch the mocked value of isGuest between tests inside one test file?
Show answer
jest.mock calls are hoisted to the top of the file and run once, so the mocked value is fixed for the whole file. Use separate test files, or a mock that reads a mutable variable.