How to make jest snapshot test log clearer with styled from MUI

Viewed 80

I am using MUI v5 + styled + jest snapshot. I would like better logs when a snapshot test failed.

Example of my component:

const styledProperties = new Set(['backgroundColor']);

const StyledAppBar = styled(AppBar, {
    shouldForwardProp: (property) => !styledProperties.has(property.toString())
})<IHeaderProperties>(({ backgroundColor, theme }) => ({
    backgroundColor: backgroundColor || theme.palette.background.paper
}));

export interface IHeaderProperties extends ICommonProperties {
    backgroundColor?: string;
}

export default function Header(properties: PropsWithChildren<IHeaderProperties>): ReactElement {
    return <StyledAppBar {...properties} />;
}

A snapshot test:

describe('given <Header />', () => {
  describe('when it is rendered', () => {
      it('should match snapshot', () => {
          renderWithProviders(<Header data-testid="Header" backgroundColor={'#FFFFFF'} />);

          const header = screen.getByTestId('Header');

          expect(header).toBeVisible();
          expect(header).toMatchSnapshot();
      });
  });
});

When the snapshot fails I only see the "css-PART_THAT_CHANGES-MuiPaper-root-MuiAppBar-root" that changed.

    expect(received).toMatchSnapshot()

    Snapshot name: `given <Header /> when it is rendered should match snapshot 1`

      -
      Snapshot - 1 +
      Received + 1

      <
      header -
      class = "... css-1adzlm4-MuiPaper-root-MuiAppBar-root" +
      class = "... css-11m65ae-MuiPaper-root-MuiAppBar-root"
    data - testid = "Header" /
      >

I rather would have something like this:

enter image description here

1 Answers

I decided to use this https://github.com/emotion-js/emotion/tree/main/packages/jest Seems to work well, heres an example of my snapshots :

exports[`given <IconButton /> when ... should match snapshot 1`] = `
.emotion-0 {
  display: -webkit-inline-box;
  display: -webkit-inline-flex;
  display: -ms-inline-flexbox;
  display: inline-flex;
  -webkit-align-items: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  position: relative;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  background-color: transparent;
  outline: 0;
  border: 0;
  margin: 0;
  border-radius: 0;
  padding: 0;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  vertical-align: middle;
  -moz-appearance: none;
  -webkit-appearance: none;
  -webkit-text-decoration: none;
  text-decoration: none;
  color: inherit;
  text-align: center;
  -webkit-flex: 0 0 auto;
  -ms-flex: 0 0 auto;
  flex: 0 0 auto;
  font-size: 1.5rem;
  padding: 8px;
  border-radius: 50%;
  overflow: visible;
  color: #fff;
  -webkit-transition: background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
  transition: background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
}

.emotion-0::-moz-focus-inner {
  border-style: none;
}

.emotion-0.Mui-disabled {
  pointer-events: none;
  cursor: default;
}

@media print {
  .emotion-0 {
    -webkit-print-color-adjust: exact;
    color-adjust: exact;
  }
}

.emotion-0:hover {
  background-color: rgba(255, 255, 255, 0.08);
}

@media (hover: none) {
  .emotion-0:hover {
    background-color: transparent;
  }
}

.emotion-0.Mui-disabled {
  background-color: transparent;
  color: rgba(255, 255, 255, 0.3);
}

.emotion-1 {
  overflow: hidden;
  pointer-events: none;
  position: absolute;
  z-index: 0;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  border-radius: inherit;
}

<button
  class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium emotion-0"
  data-testid="IconButton"
  tabindex="0"
  type="button"
>
  <span
    class="MuiTouchRipple-root emotion-1"
  />
</button>
`;

Related