npmreactjs95% confidence\u2191 149

SyntaxError with Jest and React and importing CSS files

Full error message
I am trying to get my first Jest Test to pass with React and Babel.

I am getting the following error:

  SyntaxError: /Users/manueldupont/test/avid-sibelius-publishing-viewer/src/components/TransportButton/TransportButton.less: Unexpected token 

    >  7 | @import '../variables.css';
          | ^

My package.json config for jest look like this: 

"babel": {
    "presets": [
      "es2015",
      "react"
    ],
    "plugins": [
      "syntax-class-properties",
      "transform-class-properties"
    ]
  },
  "jest": {
    "moduleNameMapper": {
      "^image![a-zA-Z0-9$_-]+$": "GlobalImageStub",
      "^[./a-zA-Z0-9$_-]+\\.png$": "RelativeImageStub"
    },
    "testPathIgnorePatterns": [
      "/node_modules/"
    ],
    "collectCoverage": true,
    "verbose": true,
    "modulePathIgnorePatterns": [
      "rpmbuild"
    ],
    "unmockedModulePathPatterns": [
      "<rootDir>/node_modules/react/",
      "<rootDir>/node_modules/react-dom/",
      "<rootDir>/node_modules/react-addons-test-utils/",
      "<rootDir>/node_modules/fbjs",
      "<rootDir>/node_modules/core-js"
    ]
  },

So what am I missing?

moduleNameMapper is the setting that tells Jest how to interpret files with different extension. You need to tell it how to handle Less files. Create a file like this in your project (you can use a different name or path if you’d like): config/CSSStub.js module.exports = {}; This stub is the module we will tell Jest to use instead of CSS or Less files. Then change moduleNameMapper setting and add this line to its object to use it: '^.+\\.(css|less)$': '<rootDir>/config/CSSStub.js' Now Jest will treat any CSS or Less file as a module exporting an empty object. You can do something else too—for example, if you use CSS Modules, you can use a Proxy so every import returns the imported property name. Read more in this guide.

API access

Get this solution programmatically \u2014 free, no authentication.

curl https://depscope.dev/api/error/d14330a3fa3bed947b2faaaf2e4e63c10fb64385c16a97a78eb1feba8317476a
hash \u00b7 d14330a3fa3bed947b2faaaf2e4e63c10fb64385c16a97a78eb1feba8317476a
SyntaxError with Jest and React and importing CSS files — DepScope fix | DepScope