Sub topic 4
Render a component with createElement
This chapter assumes you know Lightning web component basics (templates, @api properties, events), modern JavaScript, and that sfdx-lwc-jest is already installed and running in your project. Here you will learn how a test turns a component class into a live element.
Why tests need createElement
In an org, the Lightning runtime builds components from markup. In a Jest test, there is no markup that mounts your component, so you create it yourself. The createElement function from the lwc module builds a DOM element from a component class. Jest runs in a simulated browser (jsdom), so the element behaves like a real one.
The three steps
Every component test follows the same pattern:
- Create the element with
createElement. - Set public properties (optional).
- Attach the element to
document.body.
Here is the counter component's test setup:
import { createElement } from 'lwc';
import Counter from 'c/counter';
describe('c-counter', () => {
it('renders with the default step', () => {
const element = createElement('c-counter', { is: Counter });
document.body.appendChild(element);
expect(element.step).toBe(1);
});
});createElement takes two arguments:
- The tag name, such as
'c-counter'. It must follow thenamespace-nameform. In a local project the namespace isc, and a camelCase folder name becomes kebab-case (contactListbecomesc-contact-list). - An options object. The
iskey holds the component class you imported.
The import c/counter resolves to the counter folder in your lwc directory. The sfdx-lwc-jest configuration maps the c/ namespace for you.
Why attaching matters
Creating an element does not render it. The component's connectedCallback runs, and the template renders, only when the element is inserted into the document. If you skip appendChild, queries on element.shadowRoot find nothing, and lifecycle code never runs.
The diagram below shows the order of events.
Set properties before attaching
Set @api properties before you call appendChild. The component then sees the right values during its first render, as it would when a parent supplies attributes.
import { createElement } from 'lwc';
import Counter from 'c/counter';
describe('c-counter', () => {
it('uses the step passed by the parent', () => {
const element = createElement('c-counter', { is: Counter });
element.step = 5;
document.body.appendChild(element);
expect(element.step).toBe(5);
});
});Setting a property after attaching also works. However, the DOM then updates asynchronously, which the next chapter covers.
Common errors
| Symptom | Likely cause |
|---|---|
| `Cannot find module 'c/counter'` | The component folder is not under a configured `lwc` directory, or the name is misspelled. |
| `element.shadowRoot` returns `null` for queries | The element was never attached to `document.body`. |
| `Invalid tag name` style errors | The tag name lacks a hyphen or uses uppercase letters. |
| Property change does not show in the DOM | The test asserts before the component re-renders. |
Exercise
Exercise
A test calls createElement('c-counter', { is: Counter }), sets element.step = 3, and then queries element.shadowRoot.querySelector('.count'). The query returns null. What is missing, and why?
Show answer
The test never calls document.body.appendChild(element). A component renders only after it is inserted into the document, so the shadow root has no content until then.
Exercise
Why should you set @api properties before attaching the element?
Show answer
The first render then uses the intended values, as it would when a parent component supplies them. Setting them afterward triggers an extra asynchronous re-render.