question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ts-jest failing for scss files

See original GitHub issue

Issue

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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ahnpnlcommented, May 15, 2020

ts-jest only handles typescript files. If you want to handle scss, you need a custom jest transformer to handle that.

2reactions
davidpaleycommented, May 20, 2020

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:

"jest": {
    "roots": [
      "<rootDir>/src"
    ],
    "modulePaths": [
      "<rootDir>/src"
    ],
    "moduleDirectories": [
      "src",
      "node_modules"
    ],
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}"
    ],
    "resolver": "jest-pnp-resolver",
    "automock": false,
    "setupFiles": [
      "react-app-polyfill/jsdom",
      "./src/setupTests.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.+(ts|tsx|js)",
      "<rootDir>/src/**/?(*.)+(spec|test).+(ts|tsx|js)"
    ],
    "testEnvironment": "jsdom",
    "testURL": "http://localhost",
    "transform": {
      "^.+\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest",
      "^.+\\.(js|jsx)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|css|scss|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$",
      "^.+\\.module\\.(css|sass|scss)$"
    ],
    "moduleNameMapper": {
      "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "json",
      "web.jsx",
      "jsx",
      "ts",
      "tsx",
      "node"
    ],
    "globals": {
      "ts-jest": {
        "diagnostics": {
          "ignoreCodes": [
            7006,
            7016
          ]
        }
      }
    }
  },

I also tried with https://www.npmjs.com/package/jest-scss-transform but without success

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found