Skip to content

[code-infra] Enable babel-plugin-display-name in vitest #17903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ module.exports = function getBabelConfig(api) {
ignoreFilenames: ['DataGrid.tsx', 'DataGridPro.tsx'],
},
],
'@mui/internal-babel-plugin-display-name',
[
'@mui/internal-babel-plugin-display-name',
{
allowedCallees: {
'@mui/x-internals/forwardRef': ['forwardRef'],
},
},
],
[
'transform-inline-environment-variables',
{
Expand Down
6 changes: 3 additions & 3 deletions packages/x-data-grid/src/DataGrid/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const configuration: GridConfiguration = {
},
};

const DataGridRaw = forwardRef(function DataGrid<R extends GridValidRowModel>(
const DataGridRaw = function DataGrid<R extends GridValidRowModel>(
inProps: DataGridProps<R>,
ref: React.Ref<HTMLDivElement>,
) {
Expand All @@ -52,7 +52,7 @@ const DataGridRaw = forwardRef(function DataGrid<R extends GridValidRowModel>(
/>
</GridContextProvider>
);
});
};

interface DataGridComponent {
<R extends GridValidRowModel = any>(
Expand All @@ -68,7 +68,7 @@ interface DataGridComponent {
* API:
* - [DataGrid API](https://mui.com/x/api/data-grid/data-grid/)
*/
export const DataGrid = React.memo(DataGridRaw) as DataGridComponent;
export const DataGrid = React.memo(forwardRef(DataGridRaw)) as DataGridComponent;
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved the forwardRef inside the memo to prevent the component name being DataGridRaw, though I don't know if this is ok. I would expect so, since the entire thing is memoized anyways.

https://app.circleci.com/pipelines/github/mui/mui-x/92158/workflows/efcac4b3-6861-4171-b28c-b67d80af2cbb/jobs/538493


DataGridRaw.propTypes = {
// ----------------------------- Warning --------------------------------
Expand Down
2 changes: 1 addition & 1 deletion packages/x-data-grid/src/tests/layout.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ describe('<DataGrid /> - Layout & warnings', () => {
'The Data Grid component requires all rows to have a unique `id` property',
reactMajor < 19 &&
'The Data Grid component requires all rows to have a unique `id` property',
reactMajor < 19 && 'The above error occurred in the <ForwardRef(DataGrid2)> component',
reactMajor < 19 && 'The above error occurred in the <DataGrid> component',
]);
expect((errorRef.current as any).errors).to.have.length(1);
expect((errorRef.current as any).errors[0].toString()).to.include(
Expand Down
2 changes: 1 addition & 1 deletion packages/x-data-grid/src/tests/slots.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe('<DataGrid /> - Slots', () => {
'MUI X: useGridRootProps should only be used inside the DataGrid, DataGridPro or DataGridPremium component.',
reactMajor < 19 &&
'MUI X: useGridRootProps should only be used inside the DataGrid, DataGridPro or DataGridPremium component.',
reactMajor < 19 && 'The above error occurred in the <ForwardRef(GridOverlay2)> component',
reactMajor < 19 && 'The above error occurred in the <GridOverlay> component',
]);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ export const TimeGridColumnContext = React.createContext<TimeGridColumnContext |
undefined,
);

if (process.env.NODE_ENV !== 'production') {
TimeGridColumnContext.displayName = 'TimeGridColumnContext';
}

export function useTimeGridColumnContext() {
const context = React.useContext(TimeGridColumnContext);
if (context === undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ export interface TimeGridRootContext {}

export const TimeGridRootContext = React.createContext<TimeGridRootContext | undefined>(undefined);

if (process.env.NODE_ENV !== 'production') {
TimeGridRootContext.displayName = 'TimeGridRootContext';
}

export function useTimeGridRootContext() {
const context = React.useContext(TimeGridRootContext);
if (context === undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ describeTreeView<
'Encountered two children with the same key, `1`',
'MUI X: The Tree View component requires all items to have a unique `id` property.',
'Alternatively, you can use the `getItemId` prop to specify a custom id for each item',
reactMajor < 19 && `The above error occurred in the <ForwardRef(TreeItem2)> component`,
reactMajor < 19 && `The above error occurred in the <ForwardRef(TreeItem2)> component`,
reactMajor < 19 && `The above error occurred in the <TreeItem> component`,
reactMajor < 19 && `The above error occurred in the <TreeItem> component`,
]);
} else {
expect(() =>
Expand All @@ -38,8 +38,7 @@ describeTreeView<
'MUI X: The Tree View component requires all items to have a unique `id` property.',
reactMajor < 19 &&
'MUI X: The Tree View component requires all items to have a unique `id` property.',
reactMajor < 19 &&
`The above error occurred in the <ForwardRef(${treeViewComponentName}2)> component`,
reactMajor < 19 && `The above error occurred in the <${treeViewComponentName}> component`,
]);
}
});
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vitest.shared.mts
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';

const CURRENT_DIR = dirname(fileURLToPath(import.meta.url));
const WORKSPACE_ROOT = resolve(CURRENT_DIR, './');

export default defineConfig({
plugins: [
react({
babel: {
plugins: [
[
'@mui/internal-babel-plugin-display-name',
{
allowedCallees: {
'@mui/x-internals/forwardRef': ['forwardRef'],
},
},
],
],
babelrc: false,
configFile: false,
Copy link
Member

Choose a reason for hiding this comment

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

👌

},
}),
],
// We seem to need both this and the `env` property below to make it work.
define: {
'process.env.NODE_ENV': '"test"',
Expand Down