DoeJoe
DoeJoe

Reputation: 51

Jest error with not transpiled node module

When trying to run a test Jest throws the encountered unexpected token error. I'm using ky http, which obviously causes the problem and not get's transpiled.

 Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    Z:\sepview.web\sepview-react\node_modules\ky\index.js:277
    export default createInstance();
    ^^^^^^

    SyntaxError: Unexpected token export

      1 | import * as React from 'react';
    > 2 | import ky from 'ky';
        | ^
      3 |
      4 | class HttpService extends React.Component {
      5 |

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
      at Object.<anonymous> (src/components/HttpService.tsx:2:1)

Test Suites: 1 failed, 1 total

Here's my package.json

{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.7",
    "@fortawesome/free-solid-svg-icons": "^5.4.2",
    "@fortawesome/react-fontawesome": "^0.1.3",
    "@types/d3": "^5.0.1",
    "bootstrap": "^4.1.3",
    "bootswatch": "^4.1.3",
    "d3": "^5.7.0",
    "d3-array": "^2.0.2",
    "jquery": "^3.3.1",
    "ky": "^0.5.1",
    "moment": "^2.22.2",
    "node-sass": "^4.9.4",
    "popper.js": "^1.14.4",
    "react": "^16.6.0",
    "react-dom": "^16.6.0",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.1",
    "typescript": "^3.1.6"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "@types/enzyme": "^3.1.14",
    "@types/enzyme-adapter-react-16": "^1.0.3",
    "@types/jest": "^23.3.9",
    "@types/node": "^10.12.2",
    "@types/react": "^16.4.18",
    "@types/react-dom": "^16.0.9",
    "@types/react-router": "^4.4.0",
    "@types/react-router-dom": "^4.3.1",
    "enzyme": "^3.7.0",
    "enzyme-adapter-react-16": "^1.6.0"
  },
  "proxy": "http://localhost:43947"
}

When adding transform or transformIgnorePatterns to package.json like this:

"jest": {
        "transform": {
          "^.+\\.js$": "babel-jest"
      },
        "transformIgnorePatterns": [
          "/node_modules/(?!(@ky)/).*/"
        ]
      },

I get this error:

Out of the box, Create React App only supports overriding these Jest options:

  • collectCoverageFrom
  • coverageReporters
  • coverageThreshold
  • globalSetup
  • globalTeardown
  • resetMocks
  • resetModules
  • snapshotSerializers
  • watchPathIgnorePatterns.

These options in your package.json Jest configuration are not currently supported by Create React App:

  • transform
  • transformIgnorePatterns

If you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.

I also have a babel.config.js which is empty atm:

module.exports = function () {
    const presets = [];
    const plugins = [];

    return {
      presets,
      plugins
    };
  }

Ejecting is not an option. I tryed solving this for + one week. Any help is heavily appreciated.

Upvotes: 5

Views: 2496

Answers (2)

peter
peter

Reputation: 1582

add

moduleNameMapper: {
  '^ky$': require.resolve('ky').replace('index.js', 'umd.js'),
},

to your jest.config.js

Upvotes: 0

Simar Singh
Simar Singh

Reputation: 351

I had a similar problem using material-ui-next-pickers which was using es6 imports in dist/

This is because material-ui-next-pickers isn’t being transpiled before running in Jest. create-react-app excludes everything from ./node_modules.

We could fix this by changing transformIgnorePatterns but it isn't supported by create-react-app as noted in the question.

However, we can still pass it as command arg to react-script test --transformIgnorePatterns \"node_modules/(?!my-module)/\"

Example

  "scripts": {
    "test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!material-ui-next-pickers)/\" --env=jsdom",
     ...

  }

Upvotes: 5

Related Questions