[tests] Fix ReactDOMAttribute-test (#35654)

In https://github.com/facebook/react/pull/35646 I thought there was a
bug in trusted types, but the bug is in jsdom.

For trusted types we still want to check the coersion and throw for a
good dev warning, but prod will also throw becuase the browser will
implicitly coerce to a string. This ensures there's no behavior
difference between dev and prod.

So the right fix is to add in the JSDOM hack that's used in
`ReactDOMSelect-test.js`.
This commit is contained in:
Ricky authored and GitHub committed 2026-01-28 16:00:40 -05:00
1 parent 90b2dd442c
commit 230772f99d
2 files changed
+8 -9

No files matched your search

+8 -7
View File
@@ -9,6 +9,13 @@
'use strict';
// Fix JSDOM. setAttribute is supposed to throw on things that can't be implicitly toStringed.
const setAttribute = Element.prototype.setAttribute;
Element.prototype.setAttribute = function (name, value) {
// eslint-disable-next-line react-internal/safe-string-coercion
return setAttribute.call(this, name, '' + value);
};
describe('ReactDOM unknown attribute', () => {
let React;
let ReactDOMClient;
@@ -171,13 +178,7 @@ describe('ReactDOM unknown attribute', () => {
const test = () =>
testUnknownAttributeAssignment(new TemporalLike(), null);
if (gate('enableTrustedTypesIntegration') && !__DEV__) {
// TODO: this still throws in DEV even though it's not toString'd in prod.
await expect(test).rejects.toThrowError('2020-01-01');
} else {
await expect(test).rejects.toThrowError(new TypeError('prod message'));
}
await expect(test).rejects.toThrowError(new TypeError('prod message'));
assertConsoleErrorDev([
'The provided `unknown` attribute is an unsupported type TemporalLike.' +
' This value must be coerced to a string before using it here.\n' +
-2
View File
@@ -76,8 +76,6 @@ export function checkAttributeStringCoercion(
attributeName: string,
): void | string {
if (__DEV__) {
// TODO: for enableTrustedTypesIntegration we don't toString this
// so we shouldn't need the DEV warning.
if (willCoercionThrow(value)) {
console.error(
'The provided `%s` attribute is an unsupported type %s.' +