Search code examples
javascriptreactjsjestjscss-modules

Jest css-modules SyntaxError: Unexpected token


I have set up my jest to allow static files after following their documentation on how to do so, but I still recieve the following error.

How can I can it to pass and create a snapshot.

Terminal Error

FAIL  src/components/Splash/Splash.test.js
  ● Test suite failed to run

    /var/www/com/src/components/shared/logo/_Logo.css:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){.logo {
                                                                                             ^

    SyntaxError: Unexpected token .

      3 | 
      4 | 
    > 5 | import logo from './_Logo.css';
      6 | import * as font from '../font/fontello.css';

Splash.test.js

import { shallow } from 'enzyme';
import { shallowToJson } from 'enzyme-to-json';
import Splash from './Splash';

it('Splash page is rendered', () => {
    const result = shallow(
        <Splash />,
    );

    expect(shallowToJson(result)).toMatchSnapshot();
});

Jest Config

  "jest": {
    "snapshotSerializers": [
      "enzyme-to-json/serializer"
    ],
    "setupTestFrameworkScriptFile": "./node_modules/jest-enzyme/lib/index.js",
    "moduleFileExtensions": [
      "js"
    ],
    "moduleDirectories": [
      "node_modules"
    ],
    "testPathIgnorePatterns": [
      "<rootDir>/node_modules/",
      "<rootDir>/app/"
    ],
    "moduleNameMapper": {
      "moduleNameMapper": {
        "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
        "\\.(css|less)$": "identity-obj-proxy"
      }
    },
    "transform": {
      "^.+\\.js$": "babel-jest"
    }
  }

Solution

  • There is a small mistake: moduleNameMapper: {moduleNameMapper{}} should just be moduleNameMapper:{}

    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
      "\\.(css|less)$": "identity-obj-proxy"
    }