Sub topic 3
Why wire adapters and Apex need mocks
This chapter assumes you know basic Lightning Web Components (templates, decorators, lifecycle hooks), how @wire and Apex imports work, Jest basics (describe, it, expect, jest.fn), and promises with async/await.
Jest runs without a Salesforce org
LWC Jest tests run in Node.js, inside a simulated DOM called jsdom. There is no browser, no Lightning platform, and no connection to an org. That is why they run in milliseconds and can run in a CI pipeline.
The cost is that anything your component imports from the platform has nothing behind it. Three kinds of import cause this:
@salesforce/apex/ContactController.getContactList, a reference to an Apex method.lightning/uiRecordApi, which exposes adapters such asgetRecord.@salesforce/schema/...and similar scoped modules.
The sfdx-lwc-jest package ships stubs for many lightning/* modules. It does not know about your Apex classes, because they exist only in your org.
What goes wrong without a mock
Suppose a component contains this:
import { LightningElement, wire } from 'lwc';
import getContactList from '@salesforce/apex/ContactController.getContactList';
export default class ContactList extends LightningElement {
@wire(getContactList) contacts;
}If a test imports this component and no mock exists for @salesforce/apex/ContactController.getContactList, Jest tries to resolve the module and fails with an error like Cannot find module '@salesforce/apex/ContactController.getContactList'. The test file never runs.
Even when a module resolves, the real platform would need a server to answer. A test that depends on a server is slow, fragile, and cannot reproduce error cases on demand.
What a mock gives you
A mock replaces the import with something you control. This gives you three things.
- Determinism. The test supplies the data, so the result is the same every run.
- Coverage of paths you cannot easily trigger. You can make Apex reject, or return an empty list, whenever you want.
- Visibility. A mock records how it was called, so you can check the arguments your component sent.
The test then checks the component's behavior: what it renders and what it asks for. It does not check whether Apex works. Apex has its own tests, written in Apex.
Two kinds of mock
Wired and imperative calls reach the component differently, so they need different mocks.
| Data source | How the component uses it | Mock you write |
|---|---|---|
| Wire adapter (`getRecord`, wired Apex) | `@wire` provisions data when the adapter emits a value | A test wire adapter with `emit` and `error` |
| Imperative Apex | The component calls the method and awaits a promise | `jest.fn` with `mockResolvedValue` or `mockRejectedValue` |
A wire adapter pushes data to the component whenever it has some. The test therefore needs a way to push data. A test wire adapter provides emit(data) and error() for that. An imperative call is a plain function that returns a promise, so a jest.fn that returns a resolved or rejected promise is enough.
Mocking a module that does not exist
Your Apex imports are not real files in your project. When you mock them with jest.mock, pass { virtual: true } as the third argument. This tells Jest to create the mock even though it cannot find a module at that path.
jest.mock(
'@salesforce/apex/ContactController.findContacts',
() => ({ default: jest.fn() }),
{ virtual: true }
);The factory returns an object with a default property because the component uses a default import. The later chapters on wired and imperative Apex show the full tests.
Summary of the reasoning
Mocks are needed because the test environment has no platform. Wire adapters and Apex are the points where your component meets the platform, so they are the places you cut the connection and substitute something you control.
Exercise
A component imports @salesforce/apex/AccountController.getAccounts and calls it imperatively inside a button click handler. Which mock replaces the import, and what option must the jest.mock call include?
Show answer
Replace it with a jest.fn returned as the module's default export, then use mockResolvedValue or mockRejectedValue to control the result. The jest.mock call must include { virtual: true } as its third argument, because the Apex module does not exist as a real file in the project.
Exercise
Why does a wired Apex method need a test wire adapter, while an imperative Apex method can use a plain jest.fn?
Show answer
A wire adapter pushes values to the component on its own, so the test needs emit and error to trigger those pushes. An imperative method is an ordinary function that returns a promise, so a jest.fn that returns a resolved or rejected promise is enough.