Sub topic 3
Testing Error and Empty-Data Paths
Assume you know LWC templates, @wire, Apex imports, promises, and basic Jest. A reliable test checks success, empty data, and failure separately. Empty data means the request succeeded but returned no records. An error means the request failed.
Testing empty and error results
A test wire adapter lets the test control what the component receives. Emit an empty array for the empty state, then call error() for the failure state.
import { createElement } from 'lwc';
import ContactList from 'c/contactList';
import getContactList from '@salesforce/apex/ContactController.getContactList';
jest.mock(
'@salesforce/apex/ContactController.getContactList',
() => {
const { createApexTestWireAdapter } = require('@salesforce/sfdx-lwc-jest');
return { default: createApexTestWireAdapter(jest.fn()) };
},
{ virtual: true }
);
const flushPromises = () => new Promise((resolve) => setTimeout(resolve, 0));
afterEach(() => {
document.body.replaceChildren();
jest.clearAllMocks();
});
it('shows the empty state', async () => {
const element = createElement('c-contact-list', { is: ContactList });
document.body.appendChild(element);
getContactList.emit([]);
await flushPromises();
expect(element.shadowRoot.querySelector('.empty')).not.toBeNull();
});
it('shows the error state', async () => {
const element = createElement('c-contact-list', { is: ContactList });
document.body.appendChild(element);
getContactList.error();
await flushPromises();
expect(element.shadowRoot.querySelector('.error')).not.toBeNull();
});Asserting inputs and wire configuration
Assertions should verify both output and input. Imperative Apex calls are Jest mocks, so toHaveBeenCalledWith checks the parameter object.
| What to verify | Example assertion |
|---|---|
| Imperative Apex input | `expect(findContacts).toHaveBeenCalledWith({ searchTerm: 'Amy' })` |
| Wire configuration | `expect(getContactList.getLastConfig()).toEqual({ accountId: '001' })` |
Check the wire configuration after the component is attached and its reactive values have been set. This catches incorrect parameter names and missing $ references.
Keeping tests isolated
Remove every component after each test. Reset mock history and implementations with jest.clearAllMocks() or jest.resetAllMocks() when implementations change. Never depend on data emitted by an earlier test.
> callout warning > A test that passes alone but fails in the full suite often leaves DOM nodes behind or retains mock state.
Common pitfalls and debugging
- Query the component’s
shadowRoot, notdocument, for elements in the template. - Call
await flushPromises()after emitting data or resolving Apex. - Mock an Apex module with
{ virtual: true }when the module is unavailable to Jest. - Test an empty array separately from
error(). They represent different user experiences. - Inspect
getLastConfig()when a wire does not run as expected.
Exercise
Which result should you emit to test a successful request with no records?
Show answer
Emit an empty array with the test wire adapter, then flush pending promises and assert the empty-state message.