47

I've been trying to set up a testing environment for my React project and I had a problem with Unexpected token when importing the CSS file. i added the moduleNameMapper to the package.json to fix it and now i'm facing this issue. Does anyone know what can i do to fix this?

> [email protected] test C:\Users\admin\Documents\myApp
> jest

FAIL _tests_\Home.test.js
  ● Test suite failed to run

    Configuration error:

    Could not locate module react-datepicker/dist/react-datepicker-cssmodules.css (mapped as identity-obj-proxy)

    Please check:

    "moduleNameMapper": {
      "/^.+\.(css|less)$/": "identity-obj-proxy"
    },
    "resolver": undefined

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.894s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

jest portion of package.json

  "jest": {
    "transform": {
      "^.+\\.jsx?$": "babel-jest"
    },
    "moduleNameMapper": {
      "\\.(s?css|less)$": "identity-obj-proxy"
    }
  },

4 Answers 4

86

make sure you have identity-obj-proxy in your devDependencies

"devDependencies": {
  "identity-obj-proxy": "^3.0.0",
  ...
}

if not, run

npm install --save-dev identity-obj-proxy
1
  • Only a little observation: adding identity-obj-proxy to "dependencies" in package.json, instead of to "devDependencies" worked for me too. But nonetheless I had to follow stackoverflow.com/questions/44942777/… accepted answer for the tests to pass.
    – Caco
    Commented Jun 3, 2021 at 14:39
3

make sure that you have identity-obj-proxy npm package in your devDependencies list.

2

In my case, the complaint came because a recent version of jest decided to enforce file extensions, and my package.json config was missing "scss"

"moduleFileExtensions": [
      "js",
      "json",
      "vue",
      "scss"
    ],
1

We fixed this error by downgrading Jest back to 20.0.4, after updating to 21.0.1 and breaking our build with this error

Not the answer you're looking for? Browse other questions tagged or ask your own question.