Sub topic 7
Mocking built-in adapters such as getRecord
This chapter assumes you can write a basic Jest test for an LWC, know how createElement attaches a component to the DOM, and have used @wire with getRecord. The goal is to drive a component that reads a record through a built-in adapter, with no org and no server.
Why built-in adapters need a stub
Built-in adapters such as getRecord come from modules like lightning/uiRecordApi. These modules call Lightning Data Service, which only exists in a running org. Under Jest there is no such service, so the real adapter cannot return data.
The @salesforce/sfdx-lwc-jest package solves this. It maps lightning/* imports to stubs. For lightning/uiRecordApi, the stub exports getRecord as a test wire adapter. A test wire adapter is a stand-in that has the same name as the real one but adds methods your test can call to push data into the component.
You do not need jest.mock for these imports. The stub is already in place.
The component under test
Here is a component that shows an account name. It wires getRecord to a property and reads the field value in a getter.
import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class RecordName extends LightningElement {
@api recordId;
@wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD] })
account;
get name() {
return this.account && this.account.data
? this.account.data.fields.Name.value
: '';
}
}The template displays the getter and an error paragraph when the wire fails:
<template>
<template lwc:if={account.data}>
<p class="name">{name}</p>
</template>
<template lwc:if={account.error}>
<p class="error">Could not load the account.</p>
</template>
</template>Emitting a record
A test wire adapter has two methods. emit(value) sends data to every component wired to the adapter. error() sends an error. Both are synchronous calls, but the component re-renders on a later microtask, so the test must wait before it checks the DOM.
import { createElement } from 'lwc';
import RecordName from 'c/recordName';
import { getRecord } from 'lightning/uiRecordApi';
const mockRecord = {
apiName: 'Account',
id: '001000000000001AAA',
fields: { Name: { value: 'Acme', displayValue: null } }
};
describe('c-record-name', () => {
afterEach(() => {
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});
it('shows the account name from getRecord', async () => {
const element = createElement('c-record-name', { is: RecordName });
element.recordId = '001000000000001AAA';
document.body.appendChild(element);
getRecord.emit(mockRecord);
await Promise.resolve();
expect(element.shadowRoot.querySelector('.name').textContent).toBe('Acme');
});
it('shows an error message when the wire fails', async () => {
const element = createElement('c-record-name', { is: RecordName });
element.recordId = '001000000000001AAA';
document.body.appendChild(element);
getRecord.error();
await Promise.resolve();
expect(element.shadowRoot.querySelector('.error')).not.toBeNull();
});
});The record shape matters. Your mock must match what getRecord returns: apiName, id, and a fields object where each field has value and displayValue. If the shape is wrong, the component's getter throws and the test fails for a reason that has nothing to do with your logic.
What happens during a test
The diagram shows the order of events. The component connects first, the test emits data, and the render happens after the microtask queue runs.
Common mistakes
- Calling
emitbefore the component is attached. The wire connects when the element enters the DOM. Append first, then emit. - Forgetting to wait. Without an
await, the assertion runs before the re-render and sees the old DOM. - Mocking
lightning/uiRecordApiyourself. The default stub already does the job. A hand-writtenjest.mockfor it can hide theemitmethod and causegetRecord.emit is not a function. - Emitting a partial record. Include every field the component reads.
Check your understanding
Exercise
A test calls getRecord.emit(mockRecord) and then immediately runs expect(element.shadowRoot.querySelector('.name').textContent).toBe('Acme'). The assertion fails because querySelector returns null. What is the most likely cause, and how do you fix it?
Show answer
The component has not re-rendered yet, because LWC renders on a microtask after the emit. Make the test async and add await Promise.resolve() (or a flushPromises helper) between emit and the assertion.
Exercise
Why does the test for lightning/uiRecordApi not need a jest.mock call, while a test for an Apex method does?
Show answer
@salesforce/sfdx-lwc-jest already maps lightning/* modules to stubs that include test wire adapters. Apex imports such as @salesforce/apex/ContactController.getContactList have no default stub, so you must mock them yourself, with { virtual: true } because the module does not exist on disk.