1

I can reference a collection of p tags in lwc-jest, but am not able to do so for a lightning-combobox's items.

This gives me values:

const cats = element.shadowRoot.querySelectorAll('p');
expect(cats.length).toBe(mockGetPicklistValues.values.length);

And this does not:

const comboboxEl = element.shadowRoot.querySelector('lightning-combobox');
console.log(JSON.stringify(comboboxEl));

How do I test the items in a lightning-combobox?

1

lwc-jest will not know how to resolve components from outside your own project. For the lightning namespaced base components, lwc-jest provides a base set of stubs. These stubs are very basic and don't replicate the logic of the real component, just mimic the exposed API properties/methods and contain a if the original component does. Here is the combobox stub.

Instead of querying for what the combobox renders, your test should verify the data passed to the combobox is expected and trust that the combobox will render that data correctly.

You can also see this section in the lwc-jest README for how to override the default stub and provide your own.

  • Thanks for this explanation. I was leaning towards the idea that I shouldn't worry about what the combobox actually displays, just confirm what data gets passed to it, but now I understand why it wouldn't be possible. – Paul Taylor Apr 21 at 9:10

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.