Sub topic 13
The __tests__ folder and Jest
This chapter assumes you know basic JavaScript (ES modules, promises), have read the earlier chapters on the HTML template and JavaScript class, and can run commands in a Salesforce DX project. You don't need prior Jest experience.
What the folder is for
A bundle can contain a __tests__ folder. It holds Jest test files for that one component. Jest is a JavaScript test runner. Salesforce provides a Jest setup for Lightning web components, so tests run on your machine in Node.js, without a Salesforce org.
force-app/main/default/lwc/
└── greetingCard/
├── greetingCard.html
├── greetingCard.js
├── greetingCard.js-meta.xml
└── __tests__/
└── greetingCard.test.jsThree points about the folder:
- The name must be exactly
__tests__, with two underscores on each side. Jest looks for tests there. - Test files end in
.test.js. By convention the file is named after the component. - The folder is optional. A component deploys and runs the same with or without it.
Setting up Jest
Install the Salesforce test tooling once per project, then run it:
npm install @salesforce/sfdx-lwc-jest --save-dev
npx sfdx-lwc-jestThe first command adds the runner as a development dependency. The second runs every test in every bundle. Add -- --watch to rerun tests when files change, or pass a path to run one file.
Reading a test
This test creates the greetingCard component, sets a public property, and checks the rendered text.
import { createElement } from 'lwc';
import GreetingCard from 'c/greetingCard';
describe('c-greeting-card', () => {
afterEach(() => {
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});
it('renders the name', () => {
const element = createElement('c-greeting-card', { is: GreetingCard });
element.name = 'Ada';
document.body.appendChild(element);
return Promise.resolve().then(() => {
const p = element.shadowRoot.querySelector('p');
expect(p.textContent).toBe('Hello, Ada!');
});
});
});Here is what each part does:
createElementbuilds a component instance outside Salesforce. The first argument is the tag name, andisreceives the class you imported fromc/greetingCard.- Setting
element.nameworks because the class marksnamewith@api. A property without@apican't be set from outside. appendChildputs the element in the test DOM, which connects the component and renders it.element.shadowRoot.querySelector('p')reads the rendered output. Tests look inside the shadow root because the component's markup is encapsulated there.afterEachremoves elements after every test so one test can't affect the next.
Common errors
- Asserting too early. Rendering after a property change is asynchronous. Check the DOM inside
Promise.resolve().then(...), and return the promise so Jest waits for it. If you forgetreturn, the test can pass without running the assertion. - Querying
elementdirectly.element.querySelector('p')finds nothing, because the content lives inelement.shadowRoot. - Forgetting cleanup. Leftover elements from earlier tests cause confusing failures.
- Wrong tag name. Use the kebab-case form with the
c-prefix, such asc-greeting-card, for a component in the defaultcnamespace.
Exercise
Exercise
A test sets element.name = 'Ada', appends the element to document.body, and immediately calls element.shadowRoot.querySelector('p').textContent. It reports the old default text Hello, World!. What is wrong, and how do you fix it?
Show answer
The DOM updates asynchronously after a property change, so the assertion ran before the re-render. Move the query and expect into Promise.resolve().then(() => { ... }) and return that promise from the test. This gives rendering one microtask to finish.
Exercise
Where must a test file for the component orderList be placed, and what must its name end with?
Show answer
Inside orderList/__tests__/, in the component's bundle folder, with a name ending in .test.js, such as orderList.test.js.