I realise that this question has been asked on a number of occasions, though the environment has changed since those questions where asked: notably, JSDom now supports custom elements.
These other questions revolved around seeking alternatives (such as using Happy Dom) as JSDom did not support custom elements at that time. However, now that JSDom does support custom elements, does anyone have any information that can solve the following error?
TypeError: Class constructor HTMLElement cannot be invoked without 'new' 1 | export default class Foo extends HTMLElement { 2 | constructor() { > 3 | super(); | ^ 4 | 5 | this._clicker = 2; 6 | } at new Foo (__tests__/fooclass.js:3:5) at Object.<anonymous> (__tests__/fooclass.test.js:7:13)
Current setup:
A reference repo is available here (now fixed):
Custom element example
class Foo extends HTMLElement {
constructor() {
super();
this._clicker = 2;
}
connectedCallback() {
this.textContent = 'My Foo Bar Element';
}
get testCallback() {
return 'hello world!';
}
set clicker(num) {
this._clicker = Number(num);
}
get clicker() {
return this._clicker;
}
}
packages.json
{
"scripts": {
"test": "jest --env=jest-environment-jsdom-sixteen"
},
"jest": {
"verbose": true
},
"devDependencies": {
"@babel/preset-env": "^7.8.4",
"babel-plugin-transform-builtin-classes": "^0.6.1",
"jest": "^25.1.0",
"jest-environment-jsdom-sixteen": "^1.0.2"
}
}
.babelrc
{
"presets": ["@babel/preset-env"]
}
version 26
this is no longer required: github.com/facebook/jest/blob/master/packages/… – Mercier