Skip to content

Commit

Permalink
fix: [#1380] Fix behavior when no argument is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
odanado committed Apr 5, 2024
1 parent 6f6a7ab commit 06dde91
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/happy-dom/src/nodes/document/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,12 @@ export default class Document extends Node {
* @param [data] Text data.
* @returns Text node.
*/
public createTextNode(data?: string): Text {
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<Text>(this, this[PropertySymbol.ownerWindow].Text, String(data));
}

Expand Down
8 changes: 6 additions & 2 deletions packages/happy-dom/test/nodes/document/Document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,12 @@ 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.', () => {
Expand Down

0 comments on commit 06dde91

Please sign in to comment.