Sub topic 11
Asserting call arguments and wire config
This chapter assumes you can already render a component under test, flush promises, and drive a mock with emit, mockResolvedValue, and mockRejectedValue. Those pieces check what the component shows. Here you check what the component sends: the arguments passed to Apex and the configuration passed to a wire adapter.
Why check inputs
A rendering test can pass while the component asks for the wrong data. A mock returns the same value whatever it receives. If the component sends { searchTerm: undefined } or a wrong record Id, the mock still answers and the list still renders. An input assertion catches that bug.
The two data sources expose their inputs differently.
| Data source | What the mock is | How you read the input |
|---|---|---|
| Imperative Apex | `jest.fn()` | `toHaveBeenCalledWith`, `mock.calls` |
| Wired Apex or built-in adapter | Test wire adapter | `getLastConfig()` |
Asserting imperative Apex arguments
Imperative Apex methods take one object of named parameters. Check that object with toHaveBeenCalledWith. Also check the call count, so a component that calls Apex twice does not pass by accident.
import { createElement } from 'lwc';
import ContactSearch from 'c/contactSearch';
import findContacts from '@salesforce/apex/ContactController.findContacts';
jest.mock(
'@salesforce/apex/ContactController.findContacts',
() => ({ default: jest.fn() }),
{ virtual: true }
);
const flushPromises = () => new Promise((resolve) => setTimeout(resolve, 0));
describe('c-contact-search arguments', () => {
afterEach(() => {
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
jest.clearAllMocks();
});
it('sends the typed term to Apex once', async () => {
findContacts.mockResolvedValue([]);
const element = createElement('c-contact-search', { is: ContactSearch });
document.body.appendChild(element);
const input = element.shadowRoot.querySelector('lightning-input');
input.value = 'Amy';
input.dispatchEvent(new CustomEvent('change'));
element.shadowRoot.querySelector('lightning-button').click();
await flushPromises();
expect(findContacts).toHaveBeenCalledTimes(1);
expect(findContacts).toHaveBeenCalledWith({ searchTerm: 'Amy' });
});
});toHaveBeenCalledWith compares the argument object recursively. An extra property on the real call makes the assertion fail. When you only care about some properties, use expect.objectContaining({ searchTerm: 'Amy' }). To inspect a call directly, read findContacts.mock.calls[0][0].
Asserting wire config
A test wire adapter remembers the last config the component gave it. Read it with getLastConfig(). The getRecord stub from lightning/uiRecordApi has this method too.
import { createElement } from 'lwc';
import RecordName from 'c/recordName';
import { getRecord } from 'lightning/uiRecordApi';
const flushPromises = () => new Promise((resolve) => setTimeout(resolve, 0));
describe('c-record-name wire config', () => {
afterEach(() => {
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});
it('requests the record that was set', async () => {
const element = createElement('c-record-name', { is: RecordName });
element.recordId = '001000000000001AAA';
document.body.appendChild(element);
await flushPromises();
const config = getRecord.getLastConfig();
expect(config.recordId).toBe('001000000000001AAA');
});
});The component might also pass a fields array. Assert only the parts that matter, for example expect(config.fields).toEqual(expect.arrayContaining([...])), so a harmless change in field order does not break the test.
Reactive config
A wire config that uses $recordId is recalculated when that property changes. To test this, change the public property, flush, and read getLastConfig() again. The adapter returns the newest config, not the first one. Without the flush, you read a stale value.
Common mistakes
- Reading
getLastConfig()before the component is attached or before the flush. The adapter has not received a config yet. - Asserting arguments without
jest.clearAllMocks()inafterEach. Calls from an earlier test count towardtoHaveBeenCalledTimes. - Passing positional arguments to
toHaveBeenCalledWith. Apex calls take one object, so write({ searchTerm: 'Amy' }), not('Amy').
Exercise
A test calls expect(findContacts).toHaveBeenCalledWith('Amy') and fails, although the component does search for Amy. What is the likely cause, and what is the fix?
Show answer
Imperative Apex methods receive one object of named parameters, so the real call is findContacts({ searchTerm: 'Amy' }). The positional 'Amy' does not match it. Change the assertion to toHaveBeenCalledWith({ searchTerm: 'Amy' }).
Exercise
How do you check which record Id a component passed to a wired getRecord, after the parent changes recordId?
Show answer
Set the new recordId on the element, await flushPromises(), then read getRecord.getLastConfig() and assert on recordId. The flush lets the reactive config update reach the adapter first.