Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

561
Views
React JS Jest causing "SyntaxError: Unexpected token ."

I am now using react JEST to test code. If a component is single, and not importing anything else, "npm test" runs smoothly, and now, I want to test multiple components together, and I immediately got this error:

SyntaxError: Unexpected token .

It seemed whenever react is importing something else, such as this one:

require( './style/fixed-data-table.css' );
require( './style/jnpr-datatable.scss' );

and then using jest is throwing the unexpected token "." error.

There must be something wrong in my settings, but where? My Package.json contains:

 "jest": {
   "unmockedModulePathPatterns": [
     "<rootDir>/node_modules/react/",
     "<rootDir>/node_modules/react-dom/",
     "<rootDir>/node_modules/react-addons-test-utils/"
   ]
 }

And the .babelrc is already in the root. Also babel-jest is included. Thanks

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Have a look at the jest docs for webpack integration. The problem is that jest cant work with other stuff then js. So you have to mock all none js files you import. The easiest way is to configure a moduleNameMapper in your jest configs.

{
  "jest": {
    "moduleNameMapper": {
      "\\.(css|scss)$": "<rootDir>/__mocks__/styleMock.js"
    }
  }
}

with a __mocks__/styleMock.js that looks like this.

module.exports = {};
over 4 years ago · Santiago Trujillo Report

0

The easiest is to add an identity-obj-proxy package:

yarn add --dev identity-obj-proxy

And use it to automatically mock CSS/SCSS modules.

Add this to the package.json:

  "jest": {
    "moduleNameMapper": {
      "\\.(css|scss)$": "identity-obj-proxy"
    }
  }

Or the following to jest.config.ts:

moduleNameMapper: {
  '\\.(css|scss)$': 'identity-obj-proxy'
}

This way (S)CSS module class names will be automatically retrieved in tests.

Here is the source.

over 4 years ago · Santiago Trujillo Report

0

  1. npm i -D identity-obj-proxy

  2. add below to jest.config.js

   moduleNameMapper: {
       "\\.(css|less|scss|sass)$": "identity-obj-proxy"
   }
  1. jest.config.js:
testEnvironment: 'jsdom',
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!