Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Invalid constructor" with custom elements #128

Open
pblzd opened this issue Sep 30, 2022 · 1 comment
Open

"Invalid constructor" with custom elements #128

pblzd opened this issue Sep 30, 2022 · 1 comment

Comments

@pblzd
Copy link

pblzd commented Sep 30, 2022

I'm trying to write tests for some web components which inherit from HTMLElement (or Element), and am working around the lack of customElements by mocking customElements.define() (with code that simply does nothing).

With that setup I run into this error:

error: TypeError: Illegal constructor.
         throw new TypeError("Illegal constructor.");
                ^
      at new Node (https://deno.land/x/[email protected]/src/dom/node.ts:89:13)
      at new Element (https://deno.land/x/[email protected]/src/dom/element.ts:515:
      at new Category (file:///app/public/javascript/category.js:15:5)

category.js looks like this (with line 15 being the call to super()):


export default class Category extends Element {
  name = "";
  color = "";

  constructor({name, color}) {
    super();
    this.className = "category-name";
    
  }

The code that triggers the exception is just new Category({name: "a name", color: "#abcdef"}).

Do you have an idea what's the issue here? Do I need an actual CustomElementRegistry? Or an actual HTMLElement? Or am I doing something wrong?

Thank you for your work and time!

@0kku
Copy link
Collaborator

0kku commented Nov 13, 2022

This is by design: if you run the same code in your browser, you'll get the same error. In the browser, you'd create custom elements using the document.createElement() API. However, deno-dom does not have support for custom element registry yet, and therefore the library does not currently provide a way to construct custom elements.

You'll also find that the constructor for your custom element is not a valid custom element constructor, so your code wouldn't work even in a browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants