Skip to content
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

How can I set css modules? #501

Open
brgndyy opened this issue Jun 23, 2023 · 0 comments
Open

How can I set css modules? #501

brgndyy opened this issue Jun 23, 2023 · 0 comments

Comments

@brgndyy
Copy link

brgndyy commented Jun 23, 2023

now I am studying with Next.js 13 and vitest, but there is an error in the toHaveStyle part.

Since I use css modules when applying css, I tried npm test by applying css modules to the button, but it doesn't work normally.

I googled and set vitest.config.ts, but it doesn't work normally. Is there any solution?

  • vitest.config.ts
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
import { resolve } from "path";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      "@": resolve(__dirname, "./src"), // src폴더 안에 __test__ 폴더 생성
    },
  },
  test: {
    globals: true,
    environment: "jsdom",
    setupFiles: "./src/__tests__/index.test.tsx",
    css: {
      modules: {
        classNameStrategy: "non-scoped",
      },
    },
  },
});
  • Header.tsx
import classes from "./Header.module.css";

export default function Header() {
  return (
    <div>
      <button className={classes.test_button}>Change to blue</button>
    </div>
  );
}
  • Header.module.css
.test_button{
    background-color: rgb(255, 0, 0);
}
  • index.test.ts
import { expect, test } from "vitest";
import { render, screen } from "@testing-library/react";
import matchers from "@testing-library/jest-dom/matchers";
import Header from "@/components/Header";

expect.extend(matchers);

test("Home", () => {
  render(<Header />);

  // find a role
  const colorButton = screen.getByRole("button", { name: "Change to blue" });

  // button's color is red
  expect(colorButton).toHaveStyle({ backgroundColor: "rgb(255, 0, 0)" });
});

What shoud I do ? please help !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant