diff --git a/packages/happy-dom/src/nodes/document/Document.ts b/packages/happy-dom/src/nodes/document/Document.ts index 75922cf9..3e11866c 100644 --- a/packages/happy-dom/src/nodes/document/Document.ts +++ b/packages/happy-dom/src/nodes/document/Document.ts @@ -1165,8 +1165,13 @@ export default class Document extends Node { * @param [data] Text data. * @returns Text node. */ - public createTextNode(data?: string): Text { - return NodeFactory.createNode(this, this[PropertySymbol.ownerWindow].Text, data); + public createTextNode(data: string): Text { + if (arguments.length < 1) { + throw new TypeError( + `Failed to execute 'createTextNode' on 'Document': 1 argument required, but only ${arguments.length} present.` + ); + } + return NodeFactory.createNode(this, this[PropertySymbol.ownerWindow].Text, String(data)); } /** diff --git a/packages/happy-dom/test/nodes/document/Document.test.ts b/packages/happy-dom/test/nodes/document/Document.test.ts index cec5f5c1..6b9332b4 100644 --- a/packages/happy-dom/test/nodes/document/Document.test.ts +++ b/packages/happy-dom/test/nodes/document/Document.test.ts @@ -1042,8 +1042,23 @@ describe('Document', () => { }); it('Creates a text node without content.', () => { - const textNode = document.createTextNode(); - expect(textNode.data).toBe(''); + // @ts-ignore + expect(() => document.createTextNode()).toThrow( + new TypeError( + `Failed to execute 'createTextNode' on 'Document': 1 argument required, but only 0 present.` + ) + ); + }); + + it('Creates a text node with non string content.', () => { + const inputs = [1, -1, true, false, null, undefined, {}, []]; + const outputs = ['1', '-1', 'true', 'false', 'null', 'undefined', '[object Object]', '']; + + for (let i = 0; i < inputs.length; i++) { + // @ts-ignore + const textNode = document.createTextNode(inputs[i]); + expect(textNode.data).toBe(outputs[i]); + } }); });