diff --git a/packages/happy-dom/src/utilities/ClassMethodBinder.ts b/packages/happy-dom/src/utilities/ClassMethodBinder.ts index d9072f11..22ed0dff 100644 --- a/packages/happy-dom/src/utilities/ClassMethodBinder.ts +++ b/packages/happy-dom/src/utilities/ClassMethodBinder.ts @@ -24,7 +24,7 @@ export default class ClassMethodBinder { */ public bind(name: string | symbol): void { // We should never bind the Symbol.iterator method as it can cause problems with Array.from() - if (this.cache.has(name) || name === Symbol.iterator) { + if (this.cache.has(name) || name === Symbol.iterator || name === 'constructor') { return; } @@ -40,6 +40,10 @@ export default class ClassMethodBinder { const descriptor = Object.getOwnPropertyDescriptor(_class.prototype, name); if (descriptor) { if (typeof descriptor.value === 'function') { + if (descriptor.value.toString().startsWith('class ')) { + // Do not bind classes + return; + } Object.defineProperty(target, name, { ...descriptor, value: descriptor.value.bind(target) diff --git a/packages/happy-dom/test/nodes/html-form-element/HTMLFormElement.test.ts b/packages/happy-dom/test/nodes/html-form-element/HTMLFormElement.test.ts index fd3f6fca..1572b5a9 100644 --- a/packages/happy-dom/test/nodes/html-form-element/HTMLFormElement.test.ts +++ b/packages/happy-dom/test/nodes/html-form-element/HTMLFormElement.test.ts @@ -38,6 +38,22 @@ describe('HTMLFormElement', () => { }); }); + describe('constructor()', () => { + it('Matches snapshot.', () => { + element.innerHTML = ` + + + + + + + + + `; + expect(element).toMatchSnapshot(); + }); + }); + for (const property of [ 'name', 'target', diff --git a/packages/happy-dom/test/nodes/html-form-element/__snapshots__/HTMLFormElement.test.ts.snap b/packages/happy-dom/test/nodes/html-form-element/__snapshots__/HTMLFormElement.test.ts.snap new file mode 100644 index 00000000..c758a558 --- /dev/null +++ b/packages/happy-dom/test/nodes/html-form-element/__snapshots__/HTMLFormElement.test.ts.snap @@ -0,0 +1,65 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`HTMLFormElement > constructor() > Matches snapshot. 1`] = ` +
+`; diff --git a/packages/happy-dom/test/nodes/html-select-element/HTMLSelectElement.test.ts b/packages/happy-dom/test/nodes/html-select-element/HTMLSelectElement.test.ts index 7a189934..d424438d 100644 --- a/packages/happy-dom/test/nodes/html-select-element/HTMLSelectElement.test.ts +++ b/packages/happy-dom/test/nodes/html-select-element/HTMLSelectElement.test.ts @@ -24,6 +24,13 @@ describe('HTMLSelectElement', () => { }); }); + describe('constructor()', () => { + it('Matches snapshot.', () => { + element.innerHTML = ''; + expect(element).toMatchSnapshot(); + }); + }); + describe('get options()', () => { it('Reflects changes as options elements are added and removed from the DOM.', () => { const option1 =