React JS Jest causing "SyntaxError: Unexpected token ." React JS Jest causing "SyntaxError: Unexpected token ." reactjs reactjs

React JS Jest causing "SyntaxError: Unexpected token ."


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 = {};


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.


The way I got away with this was by adding these two lines to my .babelrc file

{    "presets": ["env", "react"],    "plugins": ["transform-class-properties"]}

and my package.json looks like this:

{  "name": "crud-redux",  "version": "0.1.0",  "private": true,  "dependencies": {  "react": "^16.4.0",  "react-dom": "^16.4.0",  "react-scripts": "1.1.4"}, "scripts": {     "start": "react-scripts start",     "build": "react-scripts build",     "test": "NODE_ENV=test jest",     "eject": "react-scripts eject"},  "devDependencies": {      "babel-jest": "^18.0.0",      "babel-loader": "^6.4.1",      "babel-plugin-transform-decorators-legacy": "^1.3.5",      "enzyme": "^2.9.1",      "jest": "^23.1.0",      "react-addons-test-utils": "^15.6.2",      "react-test-renderer": "^15.6.2",      "redux-mock-store": "^1.5.1",      "webpack": "^1.15.0",      "webpack-dev-server": "^1.16.5"  }}