Categories
Projects

How to prevent Material-UI Console Errors for DataGridPro License in Unit Tests

If you use DataGridPro (formerly known as XGrid) from material-ui, you need to have a license key. If you don’t provide it at the time the component renders, it will render a watermark and log errors in your console.

When unit testing this component with Jest, since I’m not validating an actual license key, my tests became polluted with a ton of console errors. I found the best approach was to mock the licensing library in @mui.

Here’s how to mock the libraries to suppress those errors in. your unit tests:

Create the mock file here:

/__mocks__/@mui/x-license-pro.js

You only need to mock the following functions (this is the entire file contents)

export function useLicenseVerifier() {
    return 'Valid';
}
export function Watermark() {
    return null;
}

Note: at the time of writing I’m using the following package versions

"jest": "^26.6.3",
"@mui/x-data-grid-pro": "^5.17.6",
"@mui/x-license-pro": "^5.17.0",

Leave a Reply

Your email address will not be published.