Skip to content

Commit

Permalink
fix: [#1640] Improves error for when a Symbol being sent to querySele…
Browse files Browse the repository at this point in the history
…ctor or querySelectorAll (#1640)
  • Loading branch information
chocolateboy authored Dec 30, 2024
1 parent f7b6ac6 commit b7f0b8f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
14 changes: 6 additions & 8 deletions packages/happy-dom/src/query-selector/QuerySelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export default class QuerySelector {
}

if (typeof selector === 'symbol') {
throw new window.TypeError(`Cannot convert a Symbol value to a string`);
throw new window.TypeError(
`Failed to execute 'querySelectorAll' on '${node.constructor.name}': Cannot convert a Symbol value to a string`
);
}

selector = String(selector);
Expand Down Expand Up @@ -205,20 +207,16 @@ export default class QuerySelector {
);
}

if (typeof selector === 'function' || typeof selector === 'symbol') {
throw new window.DOMException(
`Failed to execute 'querySelector' on '${node.constructor.name}': '${selector}' is not a valid selector.`
);
}

if (typeof selector === 'function') {
throw new window.DOMException(
`Failed to execute 'querySelector' on '${node.constructor.name}': '${selector}' is not a valid selector.`
);
}

if (typeof selector === 'symbol') {
throw new window.TypeError(`Cannot convert a Symbol value to a string`);
throw new window.TypeError(
`Failed to execute 'querySelector' on '${node.constructor.name}': Cannot convert a Symbol value to a string`
);
}

selector = String(selector);
Expand Down
8 changes: 6 additions & 2 deletions packages/happy-dom/test/query-selector/QuerySelector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ describe('QuerySelector', () => {
)
);
expect(() => container.querySelectorAll(<string>(<unknown>Symbol('test')))).toThrow(
new Error(`Cannot convert a Symbol value to a string`)
new TypeError(
`Failed to execute 'querySelectorAll' on 'HTMLDivElement': Cannot convert a Symbol value to a string`
)
);
expect(() => container.querySelectorAll(<string>(<unknown>true))).not.toThrow();
});
Expand Down Expand Up @@ -1225,7 +1227,9 @@ describe('QuerySelector', () => {
)
);
expect(() => container.querySelector(<string>(<unknown>Symbol('test')))).toThrow(
new Error(`Cannot convert a Symbol value to a string`)
new TypeError(
`Failed to execute 'querySelector' on 'HTMLDivElement': Cannot convert a Symbol value to a string`
)
);
expect(() => container.querySelector(<string>(<unknown>true))).not.toThrow();
});
Expand Down

0 comments on commit b7f0b8f

Please sign in to comment.