Repository navigation
[test-utils] Support root selection in conformance tests - #1851
Conversation
Deploy previewBundle sizeTotal Size Change: 0B(0.00%) - Total Gzip Change: 0B(0.00%) Show details for 77 more bundles@mui/internal-docs-infra/abstractCreateDemo parsed: 0B(0.00%) gzip: 0B(0.00%) PerformanceTotal duration: 18.58 ms 🔺+3.93 ms(+26.8%) | Renders: 5 (+0) | Paint: 72.09 ms 🔺+17.81 ms(+32.8%)
5 tests within noise — details Metric alarms
Check out the code infra dashboard for more information about this PR. |
There was a problem hiding this comment.
🟡 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 | nulltoConformanceOptionsand 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
defaultGetRootElementcurrently returnscontainer.firstChild, which is typed asChildNode | nulland can also be a text node. SincegetRootElementis typed asElement | null, usefirstElementChild(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
getRootElementcan returnnull, but this assertion calls.to.have.attributeon 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:
getRootElementis nullable but used as non-null. Add an explicit guard so failures point directly to a missing/incorrectgetRootElementimplementation.
expect(getRootElement(result)).to.have.attribute(testProp, 'testProp');
packages/test-utils/src/describeConformance.tsx:645
getRootElementis 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,
getRootElementis nullable but asserted on directly in theelsebranch. 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); |
There was a problem hiding this comment.
Would it be possible/make sense to mimic testClassName way of finding the root?
There was a problem hiding this comment.
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.
Summary
Add an optional
getRootElement(renderResult)callback todescribeConformancefor 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
firstChildfor the test container. The new option will let Material UI remove that adapter after a package update.Tests