Sub topic 1
Setting up Jest for LWC
This chapter gets a Jest test environment running for Lightning Web Components (LWC). Later chapters build on it to mock wire adapters and Apex calls. The goal here is a project where npm run test:unit runs and passes, with no Salesforce org connection.
Prerequisites
This chapter assumes you already know:
- Basic Lightning Web Components: templates, decorators, lifecycle hooks.
- How
@wireworks and how Apex methods are imported in LWC. - Basic Jest:
describe,it,expect,jest.fn. - JavaScript promises and
async/await.
You also need Node.js and npm installed, and a Salesforce DX project (an sfdx-project.json at the root).
What sfdx-lwc-jest provides
LWC components import things that exist only on the Salesforce platform, such as lightning/uiRecordApi or @salesforce/apex/.... Plain Jest cannot resolve these. The @salesforce/sfdx-lwc-jest package fixes this in three ways:
- It configures Jest to compile LWC syntax, including decorators and HTML templates.
- It supplies stub modules for base components such as
lightning-inputand for platform modules such aslightning/uiRecordApi. - It provides test helpers, such as
createApexTestWireAdapter, that later chapters use.
Installing and configuring
From the project root, install the package as a development dependency:
npm install --save-dev @salesforce/sfdx-lwc-jestNext, create a jest.config.js file in the project root. The package exports a ready-made configuration that you extend:
const { jestConfig } = require('@salesforce/sfdx-lwc-jest/config');
module.exports = {
...jestConfig,
modulePathIgnorePatterns: ['<rootDir>/.localdevserver']
};Then add scripts to package.json:
{
"scripts": {
"test:unit": "sfdx-lwc-jest",
"test:unit:watch": "sfdx-lwc-jest -- --watch",
"test:unit:coverage": "sfdx-lwc-jest -- --coverage"
}
}Arguments after the -- pass through to Jest itself.
Where test files go
Jest finds tests inside a __tests__ folder next to the component. Name each file after the component with a .test.js suffix:
force-app/main/default/lwc/contactList/
contactList.html
contactList.js
__tests__/
contactList.test.jsThe c/contactList import in a test resolves because the Jest configuration maps the c namespace to your component folders.
How a test run flows
The diagram shows what happens when you run the script.
A first smoke test
Create __tests__/contactList.test.js and confirm the setup works. This test only creates the component and checks that it has a shadow root. It mocks no data source yet.
import { createElement } from 'lwc';
import ContactList from 'c/contactList';
describe('c-contact-list setup', () => {
afterEach(() => {
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});
it('creates the component', () => {
const element = createElement('c-contact-list', { is: ContactList });
document.body.appendChild(element);
expect(element.shadowRoot).not.toBeNull();
});
});Run npm run test:unit. If the component imports an Apex method and you have not mocked it, the test fails with a "Cannot find module" error. That failure is expected and motivates the mocks in the next chapters.
Common setup problems
| Symptom | Likely cause | Fix |
|---|---|---|
| `Cannot find module 'c/contactList'` | Test file is outside the component folder tree, or the config is missing | Check `jest.config.js` and the file location |
| `Cannot find module '@salesforce/apex/...'` | The Apex import is not mocked | Add a virtual mock, covered later |
| No tests found | The file is not in `__tests__` or lacks `.test.js` | Rename or move it |
Exercise
Why does a test fail with "Cannot find module '@salesforce/apex/ContactController.getContactList'" even though sfdx-lwc-jest is installed?
Show answer
The sfdx-lwc-jest package stubs many platform modules, but Apex classes are specific to your org and are not stubbed. The test must mock the import itself, with jest.mock and the { virtual: true } option, because the module does not exist on disk.