styled-components / jest-styled-components

🔧 💅 Jest utilities for Styled Components
MIT License
1.58k stars 144 forks source link

TypeError: global.beforeEach is not a function #431

Closed supminn closed 4 months ago

supminn commented 11 months ago

I am new to jest-styled-components. I have created a react component and added styles using styled-components However, while adding the following test, I am getting these errors:

Component

const StyledImage = styled.img`
  height: auto;
  margin: 0.5rem;
  border-radius: 0.5rem;
  width: ${(props) => {
    switch (props.size) {
      case "small":
        return "6rem";
      case "medium":
        return "12rem";
      case "large":
        return "14rem";
      default:
        return "12rem";
    }
  }};
`;
function Image({ src, title, size }) {
  return <StyledImage src={src} alt={title} size={size} />;
}

Test file

import { render, screen } from "@testing-library/react";
import { Image } from "./Image";
import { describe, expect, test } from "vitest";
import matchers from "@testing-library/jest-dom/matchers";
expect.extend(matchers);
import "jest-styled-components";

describe("Image", () => {
  test("renders an image with the correct size", () => {
    render(<Image src="test.jpg" title="Test image" size="small" />);
    const imageElement = screen.getByAltText(/test image/i);
    expect(imageElement).toBeInTheDocument();
    expect(imageElement).toHaveStyleRule("width: 6rem");
  });
});

Package version

"styled-components": "^6.0.6"
"jest-styled-components": "^7.1.1",

Error Message screenshot

Screenshot 2023-08-03 at 9 12 48 PM

Note: I tried to downgrade a few versions of jest-styled-components and styled-components but could not surpass this issue. Could you please share the possible fixes.

khusharth commented 11 months ago

@supminn Can you please share your jest configuration file / or a project link where this is reproducible?

supminn commented 11 months ago

Hi @khusharth Thanks for reaching out. Here's my package https://github.com/supminn/monorepo-lerna-demo/tree/master/packages/app-atoms (app-atoms).

I have created a react app using Vite. I tried to create unit tests using vitest. Since I have used styled-components, I was trying to test the styles using jest-styled-components.

Do let me know how I can fix this issue. Thanks.

jalooc commented 10 months ago

That's because Vitest by default does not expose test, expect, beforeEach etc. as globals, like jest does. You can make it easily work by changing that with a configuration: https://vitest.dev/config/#globals

However I'd love to see jest-styled-components enable an option to set up the before hooks without needing to expose globals, because not having globals is preferable in general.

me4502 commented 7 months ago

These expect calls would also hit issues without globals, https://github.com/styled-components/jest-styled-components/blob/main/src/utils.js#L72-L74

So it's more than just the hooks sadly

guidsdo commented 4 months ago

I found the solution in @ggdaltoso's

{
  "test": {
    "globals": true,
    "setupFiles": ["jest-styled-components"]
  }
}
supminn commented 4 months ago

I found the solution in @ggdaltoso's

{
  "test": {
    "globals": true,
    "setupFiles": ["jest-styled-components"]
  }
}

This worked! Thank you soo much. Closing this issue.