笑话找不到SCSS模块

时间:2020-05-07 09:01:40

标签: reactjs sass jestjs create-react-app css-modules

这是我的代码(只是一个非常基本的设置)。所有文件都在同一个文件夹中。
我正在使用create-react-app

User.tsx:

import React from "react";
import styles from "./User.module.scss";

const User: React.FC = () => {
  return <div className={styles.user}>User</div>;
};

export default User;

User.module.scss:

@import "scss/abstracts";

.user {
  color: $dark_theme_color;
}

User.test.tsx:

import React from "react";
import { render, getByText } from "@testing-library/react";
import User from "./User";

it("renders correctly", () => {
  const { asFragment } = render(<User />);
  const fragmentElement = asFragment().firstChild as HTMLElement;
  const root = getByText(fragmentElement, "User");

  expect(root).not.toBe(null);
  expect(root).toMatchSnapshot();
});

运行jest时出现此错误:

FAIL  src/pages/User/User.test.tsx
   ● Test suite failed to run

src/pages/User/User.tsx:2:20 - error TS2307: Cannot find module './User.module.scss'.

2 import styles from "./User.module.scss";

jest.config.js:

module.exports = {
  roots: ["<rootDir>/src"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  setupFilesAfterEnv: [
    "@testing-library/react/cleanup-after-each",
    "@testing-library/jest-dom/extend-expect"
  ],
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
  moduleNameMapper: {
    "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
  }
};

我已经尝试了Google搜索结果的前两页中的几乎所有解决方案,例如:

  • 使用__mocks__/styleMock.js
  • jest.mock("./User.module.scss")
  • jest-transform-css
  • jest-css-modules-transform
  • jest-css-modules
  • import * as styles from "./User.module.scss";
  • declare module "*.scss";中添加了globals.d.ts

但没有任何效果。
请帮忙。

0 个答案:

没有答案