4

I have created a native custom element as web component with pure JS/HTML/CSS following the guide here.

Now I am wondering how can I write unit tests for such a component. There is an excellent library web component tester but I believe it is only meant for the components created with polymer.

As my component is not a polymer web component, rather a native one, can somebody please point me to the right direction for unit testing.

Waku-2
  • 1,136
  • 2
  • 13
  • 26

1 Answers1

6

You can use Polymer's web-component-tester for vanilla components. This blog post has some examples: https://bendyworks.com/blog/native-web-components.

You can test your component something like this:

  <my-vanilla-component></my-vanilla-component>
  <script>
    suite('<my-vanilla-component>', function() {
      let component = document.querySelector('my-vanilla-component');
      test('renders div', () => {
        assert.isOk(component.shadowRoot.querySelector('div'));
      });
    });
  </script>
MauroPorras
  • 5,079
  • 5
  • 30
  • 41
  • How does this work under the hood? How does it see the rendered form of the page, when (for example) shadowRoot only shows the shadow DOM before slots have been filled? – Michael Jun 07 '22 at 21:55