jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.21k stars 6.46k forks source link

Problems with create-react-app + typescript + CSS Modules + SASS #10024

Closed davidpaley closed 1 year ago

davidpaley commented 4 years ago

I am using a React app using create-react-app, CSS Modules with SASS, and Typescript. For files that do not need styles, the automated tests work great. However, when I test some file that has styles I get this error: image

Some relevant files related with this:

jest.setMock('cross-fetch', fetchMock);

const localStorageMock = { getItem: jest.fn(), setItem: jest.fn(), clear: jest.fn(), }; global.localStorage = localStorageMock;

- tsconfig.json

{ "compilerOptions": { "outDir": "./dist/", "sourceMap": true, "noImplicitAny": true, "module": "commonjs", "target": "es6", "jsx": "react", "esModuleInterop": true }, "include": ["./src/*/"] }



## Expected behavior
Execute the tests using CSS Modules correctly.
davidpaley commented 4 years ago

Just for the record, I could fix this setting diagnostics to false:

"globals": {
      "ts-jest": {
        "diagnostics": false
      }
    }

I am also using babel-jest for the moduleNameMapper

"moduleNameMapper": {
      "^.+\\.(css|less|scss)$": "babel-jest"
    },
ahnpnl commented 4 years ago

For anyone who runs into this issue, I have created a repo to demonstrate how to fix this issue besides the workaround above by @davidpaley . You can find it here https://github.com/ahnpnl/react-ts-jest-babel

jayarjo commented 1 year ago

@ahnpnl your link is not available anymore. @davidpaley your approach worked, however I'm not sure why. Do you know why?

github-actions[bot] commented 1 year ago

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.