It’s easy to create a custom element and forget that HTML elements can be created programmatically with the DOM’s createElement() method. Consumers of your custom elements might do this or they might use a framework that will.

So, a great smoke test for custom elements is to try to create it programmatically:

document.createElement("my-element");

There’s a couple of exceptions this might throw:

DOMException: Failed to construct 'CustomElement': The result must not have children

In this case, you’re adding children to your custom element inside its constructor.

DOMException: Failed to construct 'CustomElement': The result must not have attributes

In this case, you’re setting attributes on your custom element inside its constructor.

The HTML parser isn’t going to surface these issues for you and so if that’s all your testing with (i.e. you’re just writing plain text HTML), then you could end up shipping something that’s broken.

An important principle emerges from these cases: don’t do light DOM operations inside of the constructor.

A digression on sciopobia

If you write “HTML Web Components” — sciophobic custom elements that avoid using the Shadow DOM — you are likely going to be more prone to this sort of issue as you’re creating elements against the grain of HTML. It might seem easy enough to just not do stuff in the constructor, however, using the light DOM to express the make up of a custom element is naturally going to be more prone to these issues in general which is why I avoid it if I can. When you ditch encapsulation this is what you get. The promise of “framework agnostic” components does not look very promising with such an approach, since frameworks expect custom elements to behave as HTML elements do.