question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@emotion/jest - react test library with emotion

See original GitHub issue

It’s my first time to use emotion and I am a bit confused about what would be the best way to test with emotion and @testing-library/react.

When I check the snapshot, it doesn’t contain any style information and feels like this is not the correct way to do it.

exports[`Brands has default styles 1`] = `
<div>
  <div
    class="css-1anixd5-Brands eciq85e2"
  />
</div>
`;
it('has default styles', () => {
    const { container } = renderWithTheme(<Container />)
    expect(container).toMatchSnapshot()
  })
})

what am I missing here? Any helps would be appreciated! thanks

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
sustainjane98commented, Feb 8, 2021

Is there any way to test an Element which defines Styling for all its direct children? I tried this in an jest@25.5.4 @testing-library/react@11.2.5 and @emotion/jest@11.1.0

/* FlexContainer.tsx */

<div
  css={css`
    & {
      margin: 8px;
    }
    & > * {
      margin: 8px;
    }
  `}
>
  {children}
</div>;

So I wrote an unit test:

/* flexContainer.spec.tsx */

expect.extend(matchers);
test("Test JustifyContent Prop", () => {
  render(
    <FlexContainer>
      <div data-testid="child" />
    </FlexContainer>,
  );
  const flexItem = screen.getByTestId(
    "child",
  );

  expect(flexItem).toHaveStyleRule(
    "margin",
    "calc(0px / 2) calc(8px / 2)",
  );
  /*Returned: No such Style Element "margin"*/
});

But somehow the @emotion/jest@11.1.0 wasn’t able to resolve styles provided by the Parent. So is there a solution to achieve this?

0reactions
sustainjane98commented, Feb 8, 2021

Please always try to share a repro case in a runnable form - either by providing a git repository to clone or a codesandbox. OSS maintainers usually can’t afford the time to set up the repro, even if exact steps are given.

Sure I will add an Codesandbox tomorrow, Issue is that there is not yet full support for nextjs 😬 if this not work I provide an Github Repo then.

Read more comments on GitHub >

github_iconTop Results From Across the Web

emotion/jest
The easiest way to test React components with emotion is with the snapshot ... but @emotion/jest also works with enzyme and react-testing-library).
Read more >
How to use jest-emotion and react-testing-library ... - Code Daily
We'll use jest-emotion to add in new matchers to Jest and combined with react-testing-library we'll be able to test our components are rendering...
Read more >
React testing library not rendering Emotion CSS
I have tried using the "@emotion/jest/serializer" but still no luck. Component: <button role="button" css={(theme)=> { backgroundColor: ...
Read more >
Testing Emotion Styles | Building SPAs - Carl Rippon
reacttests. Emotion is a popular CSS-in-JS library for React. One of the things I love about it is the ability to declare conditional...
Read more >
@emotion/jest - npm
@emotion/jest. TypeScript icon, indicating that this package has built-in type declarations · Keywords.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found