5

I'm exploring how to write unit tests for an existing LWC. It's clear that I can write assertions for public properties and DOM elements -- but is there a way to write assertions on other properties, like those marked with @track?

For example, the component has five @api properties and 40 @track properties. I'd like to write tests to assert the state on all of them (separately, of course).

Apex has the @TestVisible annotation. Is there something like that for LWC? Or another approach anyone can recommend?

1
  • Documentation Write Jest Tests for Lightning Web Components has an example in the section Test Asynchronous DOM Updates; where they mention you can create and render the element then change the properties which are marked as api or track. In case this isn't helpful, please share a more concrete example or scenario illustrating the problem.
    – Raul
    Apr 26, 2020 at 22:19

1 Answer 1

9

As can be inferred from the documentation (cited by Raul) Jest is about "black box" testing. You have access to the component API, any external data sources (for example wires and imperative Apex) and the component's output (the rendering), nothing more.

Your tracked properties will, themselves, be assigned values based on the component API values and external data sources, and contribute to the rendering. They do not, in themselves, need specific testing - it is their effect that you test.

The blog article referenced by the documentation should help you piece this together further.

1
  • That put things perfectly into perspective for me thanks Aug 29 at 23:14

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.