In a LWC project, I'm setting certain child component style properties from the parent component. I want to test that an element in a child component shows up with those properties when it's rendered, based on a public reactive property that has been set.

The child component:


    <div style={headerStyle}>
        Some content...


import { LightningElement, api } from 'lwc';

export default class MyComponent extends LightningElement {
    @api backgroundColor;

    get headerStyle(){
        return 'background-color:' + this.backgroundColor';';


import { createElement } from 'lwc';
import myComponent from 'c/myComponent';

describe('myComponent tests', () => {

    it('renders the background color correctly', () => {

        const element = createElement('c-my-component', {
            is: myComponent

        element.backgroundColor = 'yellow';

        // Add the component to the DOM

        // Get the div
        let div = element.shadowRoot.querySelector('div');

        // Await async DOM updates
        return Promise.resolve()
            .then(() => {
                // ???



Stumbled across the answer as I was writing up the question.

You can access an element's style properties ( ...drum roll... ) via its style property.

return Promise.resolve()
            .then(() => {
                // This passes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.