Skip to content

[test-utils] Support root selection in conformance tests - #1851

Merged
LukasTy merged 2 commits into
mui:masterfrom
LukasTy:codex/conformance-root-element
Sep 16, 2026
Merged

LukasTy merged 2 commits into
mui:masterfrom
LukasTy:codex/conformance-root-element

Conversation

@LukasTy

@LukasTy LukasTy commented Sep 10, 2026

Copy link
Copy Markdown
Member

Summary

Add an optional getRootElement(renderResult) callback to describeConformance for components that render through a portal or inside a wrapper.

Root-class, default-prop, and root-style assertions use the callback. The default remains renderResult.container.firstChild. The render result and its real DOM container stay unchanged. Tests that render multiple instances keep their own queries.

This addresses a review concern in mui/material-ui#48823. Its current adapter substitutes an object with only firstChild for the test container. The new option will let Material UI remove that adapter after a package update.

Tests

  • Add 30 conformance tests for inline, wrapped, and portal roots with sync and async render functions.
  • Check that root selection receives a real container with working DOM methods.
  • Confirm that four wrapped-root and portal-root tests fail before the fix and pass after it.
  • Pass all 42 tests in the test-utils package.
  • Test a temporary copy of the updated helper with Material UI: 812 Chromium tests and 532 jsdom tests pass across Menu2 and classic button components. Remove the temporary copy after the checks.
  • Pass the repository build, TypeScript, ESLint, and formatting checks. Run the Material UI prose checks.

@code-infra-dashboard

code-infra-dashboard Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-1851--mui-internal.netlify.app/
QR code for https://deploy-preview-1851--mui-internal.netlify.app/

Bundle size

Total Size Change: 0B(0.00%) - Total Gzip Change: 0B(0.00%)
Files: 77 total (0 added, 0 removed, 0 changed)

Show details for 77 more bundles

@mui/internal-docs-infra/abstractCreateDemo parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/abstractCreateDemoClient parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/abstractCreateStream parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/abstractCreateTypes parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/ChunkProvider parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/cli parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CodeControllerContext parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CodeExternalsContext parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CodeHighlighter parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CodeHighlighter/errors parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CodeHighlighter/types parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CodeProvider parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CoordinatedLazy parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/CoordinatedLazy/types parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/createDemoData parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/createDemoData/types parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/createSitemap parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/createSitemap/types parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useCode parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useCodeWindow parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useCoordinated parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useCopier parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useDemo parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useDemoController parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useErrors parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useLocalStorageState parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/usePreference parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useScrollAnchor parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useSearch parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useSearch/types parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useStream parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useStream/types parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useType parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useTypes parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/useUrlHashState parsed: 0B(0.00%) gzip: 0B(0.00%)
@mui/internal-docs-infra/withDocsInfra parsed: 0B(0.00%) gzip: 0B(0.00%)
addLineGutters parsed: 0B(0.00%) gzip: 0B(0.00%)
applyCodeTransformWithComments parsed: 0B(0.00%) gzip: 0B(0.00%)
BuildEngine parsed: 0B(0.00%) gzip: 0B(0.00%)
CodeEditor parsed: 0B(0.00%) gzip: 0B(0.00%)
CodeHighlighterChunk parsed: 0B(0.00%) gzip: 0B(0.00%)
CodeHighlighterClient parsed: 0B(0.00%) gzip: 0B(0.00%)
CodeInitialSourceLoader parsed: 0B(0.00%) gzip: 0B(0.00%)
CodeSourceLoader parsed: 0B(0.00%) gzip: 0B(0.00%)
compileCssWithPostcss parsed: 0B(0.00%) gzip: 0B(0.00%)
computeHastDeltas parsed: 0B(0.00%) gzip: 0B(0.00%)
createFrame parsed: 0B(0.00%) gzip: 0B(0.00%)
createParseSourceWorkerClient parsed: 0B(0.00%) gzip: 0B(0.00%)
embedTransforms parsed: 0B(0.00%) gzip: 0B(0.00%)
enhanceCodeEmphasis parsed: 0B(0.00%) gzip: 0B(0.00%)
findExpandingRanges parsed: 0B(0.00%) gzip: 0B(0.00%)
frameVisibility parsed: 0B(0.00%) gzip: 0B(0.00%)
getHastTextContent parsed: 0B(0.00%) gzip: 0B(0.00%)
grammarCache parsed: 0B(0.00%) gzip: 0B(0.00%)
grammarLoaders parsed: 0B(0.00%) gzip: 0B(0.00%)
grammarMaps parsed: 0B(0.00%) gzip: 0B(0.00%)
grammars parsed: 0B(0.00%) gzip: 0B(0.00%)
hastUtils parsed: 0B(0.00%) gzip: 0B(0.00%)
isFrameSpan parsed: 0B(0.00%) gzip: 0B(0.00%)
loadCodeFallback parsed: 0B(0.00%) gzip: 0B(0.00%)
loadIsomorphicCodeVariant parsed: 0B(0.00%) gzip: 0B(0.00%)
parseSource parsed: 0B(0.00%) gzip: 0B(0.00%)
rolldown-runtime parsed: 0B(0.00%) gzip: 0B(0.00%)
source.css parsed: 0B(0.00%) gzip: 0B(0.00%)
source.js parsed: 0B(0.00%) gzip: 0B(0.00%)
source.json parsed: 0B(0.00%) gzip: 0B(0.00%)
source.mdx parsed: 0B(0.00%) gzip: 0B(0.00%)
source.shell parsed: 0B(0.00%) gzip: 0B(0.00%)
source.ts parsed: 0B(0.00%) gzip: 0B(0.00%)
source.tsx parsed: 0B(0.00%) gzip: 0B(0.00%)
source.yaml parsed: 0B(0.00%) gzip: 0B(0.00%)
SourceEditingEngine parsed: 0B(0.00%) gzip: 0B(0.00%)
text.html.basic parsed: 0B(0.00%) gzip: 0B(0.00%)
text.md parsed: 0B(0.00%) gzip: 0B(0.00%)
TransformEngine parsed: 0B(0.00%) gzip: 0B(0.00%)
transpileSource parsed: 0B(0.00%) gzip: 0B(0.00%)
useGrammarsReady parsed: 0B(0.00%) gzip: 0B(0.00%)

Details of bundle changes

Performance

Total duration: 18.58 ms 🔺+3.93 ms(+26.8%) | Renders: 5 (+0) | Paint: 72.09 ms 🔺+17.81 ms(+32.8%)

Test Duration Renders
HeavyList mount 10.05 ms 🔺+3.47 ms(+52.7%) 1 (+0)
DataGrid mount with paint timing 2.24 ms 🔺+0.40 ms(+21.7%) 1 (+0)

5 tests within noise — details

Metric alarms

Test Metric Change
HeavyList mount bench:paint 🔺 +18.50 ms

Check out the code infra dashboard for more information about this PR.

@LukasTy LukasTy added type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. scope: code-infra Involves the code-infra product (https://www.notion.so/mui-org/5562c14178aa42af97bc1fa5114000cd). test labels Sep 10, 2026
@LukasTy LukasTy self-assigned this Sep 10, 2026
@LukasTy
LukasTy requested review from Janpot and brijeshb42 and a lite review from Copilot September 10, 2026 15:04
@LukasTy
LukasTy marked this pull request as ready for review September 10, 2026 15:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new default root selector and getRootElement usage currently have type-safety and null-handling issues that can cause TypeScript errors or unclear runtime failures in conformance assertions.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR extends describeConformance in @mui/test-utils with an optional getRootElement(renderResult) callback so conformance assertions that assume a single “root” can target the correct element for wrapped or portal-rendered components.

Changes:

  • Add getRootElement?: (result) => Element | null to ConformanceOptions and wire it into root-class, theme default-props, and theme style-override assertions.
  • Introduce a default root selector (currently result.container.firstChild) intended to preserve existing behavior.
  • Add a new conformance test suite covering inline, wrapped, and portal roots across sync/async render paths.
File summaries
File Description
packages/test-utils/src/describeConformance.tsx Adds the getRootElement option and updates several assertions to use it for root targeting.
packages/test-utils/src/describeConformance.test.tsx Adds tests validating root selection behavior for inline/wrapped/portal layouts with sync and async render.
Review details

Suppressed comments (5)

packages/test-utils/src/describeConformance.tsx:102

  • defaultGetRootElement currently returns container.firstChild, which is typed as ChildNode | null and can also be a text node. Since getRootElement is typed as Element | null, use firstElementChild (and add an explicit return type) so the default is type-safe and always an element when present.
function defaultGetRootElement(result: MuiRenderResult) {
  return result.container.firstChild;
}

packages/test-utils/src/describeConformance.tsx:537

  • getRootElement can return null, but this assertion calls .to.have.attribute on the return value directly. Add a small null guard so failures produce a clear, actionable error message.
      const result = await render(<ThemeProvider theme={theme}>{element}</ThemeProvider>);

      expect(getRootElement(result)).to.have.attribute(testProp, 'testProp');

packages/test-utils/src/describeConformance.tsx:575

  • Same as the ThemeProvider case: getRootElement is nullable but used as non-null. Add an explicit guard so failures point directly to a missing/incorrect getRootElement implementation.

      expect(getRootElement(result)).to.have.attribute(testProp, 'testProp');

packages/test-utils/src/describeConformance.tsx:645

  • getRootElement is nullable but used directly in a computed-style assertion. Add a null guard with a clear message so misconfigured root selection fails in an actionable way.
      expect(getRootElement(result)).to.toHaveComputedStyle(testStyle);

packages/test-utils/src/describeConformance.tsx:718

  • In the slots styleOverrides test, getRootElement is nullable but asserted on directly in the else branch. Add a null guard so a bad selector (common with wrappers/portals) reports a clear failure.
      } else {
        expect(getRootElement(result)).to.toHaveComputedStyle(testStyle);
      }
  • Files reviewed: 2/2 changed files
  • Comments generated: 3
  • Review effort level: Lite

if (!skip || !skip.includes('classesRoot')) {
// Test that classes prop works
expect(container.firstChild).to.have.class(classesRootClassname);
expect(root).to.have.class(classesRootClassname);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible/make sense to mimic testClassName way of finding the root?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this approach. It works for Menu2, but classic Switch puts data-testid on its inner switch base and className on its outer root. Using the same query would change which element the root-class test checks. The optional callback preserves existing behavior and lets portal components select their root without replacing the render container. I think keeping it is the smaller change.
Unless you think a substantial refactor is worth it, I suggest sticking with this opt-in approach.

@LukasTy
LukasTy merged commit 1a52cef into mui:master Sep 16, 2026
16 checks passed
@LukasTy
LukasTy deleted the codex/conformance-root-element branch September 16, 2026 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: code-infra Involves the code-infra product (https://www.notion.so/mui-org/5562c14178aa42af97bc1fa5114000cd). test type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants