ts-jest failing for scss files
See original GitHub issueIssue
I get the following error when I run ts-jest
:
error TS2307: Cannot find module '../styles/styles.scss'
import css from '../styles/styles.scss'
My jest.config.js file is as follows:
module.exports = {
testEnvironment: "node",
preset: 'ts-jest',
setupFilesAfterEnv: ["<rootDir>/__tests__/setup-enzyme.ts"],
transform: {
"^.+\\.tsx?$": "ts-jest"
},
testRegex: "(/__tests__/.*|(\\.|/).*spec)\\.tsx$",
moduleFileExtensions: [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
snapshotSerializers: ["enzyme-to-json/serializer"],
globals: {
"ts-jest": {
"tsConfig": "<rootDir>/tsconfig.jest.json"
}
},
moduleNameMapper:{
"\\.(css|less|sass|scss)$": "<rootDir>/__mocks__/styleMock.js",
}
};
It does not pick up the mocks I have specified for scss files. I need to explicitly specify the following configuration in order for the tests to pass:
globals: {
"ts-jest": {
"tsConfig": "<rootDir>/tsconfig.jest.json",
diagnostics: false
}
},
Versions:
“jest”: “^25.1.0”, “ts-jest”: “^25.2.0”, “@types/jest”: “^25.1.2”,
Expected Behaviour
The tests should pick up the mocks mentioned in the moduleNameMapper
section of the jest.config.js file even when diagnostics: true
.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:14 (2 by maintainers)
Top Results From Across the Web
SyntaxError with Jest and React and importing CSS files
My package. json config for jest look like this: "babel": { "presets": [ "es2015", "react" ], "plugins": [ "syntax-class-properties", "transform-class- ...
Read more >Configuring Jest
To read TypeScript configuration files Jest requires ts-node . ... For example, with the following configuration jest will fail if there is ...
Read more >[Solved]-CSS module @import fails the Jest test suit-Reactjs
I was importing CSS without the extension in my components and Jest was confusing that import with JS file. Solution is instead of...
Read more >ts-jest cannot find module | The Search Engine You Control
globals: { 'ts-jest': { /* Fails on mapped import syntax without this. ... (css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub', '^.
Read more >Webpack in 2022: TypeScript + Jest + Sass + ESLint
It's a webpack loader, which will explain to webpack what to do with ts files. npm install --save-dev typescript ts-loader. After installation, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
ts-jest
only handles typescript files. If you want to handlescss
, you need a custom jest transformer to handle that.I am using the same aproach that you @ahnpnl shared for my project, using similar type of transform and it is still not working for me. @anshulguleria could you fix this?
This is my package.json config:
I also tried with https://www.npmjs.com/package/jest-scss-transform but without success