jest - Unexpected token 'export' for importing external library

Viewed 645

I am going to test below component

WalletButton.jsx

import React from 'react';

import { WalletDialogButton } from "@solana/wallet-adapter-material-ui";

function WalletButton() {
    return (
        <WalletDialogButton>
          {"CONNECT"}
        </WalletDialogButton>
      );
}

export default WalletButton;

The test case is below: wallet-button.test.js

import {shallow} from "enzyme"
import React from 'react'

import WalletButton from "./WalletButton";

it('render WalletButton Component', () => {
    expect(shallow(<WalletButton/>)).toMatchSnapshot();
})

When I run npm test, it shows error

  ● 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:

    /Users/CCCC/Desktop/SourceTree/my-project/node_modules/@solana/wallet-adapter-material-ui/lib/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './useWalletDialog';

enter image description here

I attempt to add

  transformIgnorePatterns: [
    "node_modules/(?!@solana/wallet-adapter-material-ui)",
  ]

in jest.config.js but still not working.

How to fix it?

jest.config.js

module.exports = {
  moduleDirectories: ["node_modules", "src"],
  moduleNameMapper: {
    "^@/(.*)$": "<rootDir>/src/$1",
    "\\.(css|scss)$": "identity-obj-proxy",
  },
  transform: {
    "^.+\\.(js|jsx)$": "babel-jest",
    ".+\\.(png|jpg|svg|ttf|woff|woff2)$": "jest-transform-stub",
  },
  transformIgnorePatterns: [
    "node_modules/(?!@solana/wallet-adapter-material-ui)",
  ],
  setupFilesAfterEnv: ["<rootDir>/src/setupTests.js"],
  snapshotSerializers: ["enzyme-to-json/serializer"],
};

Update 1
babelrc

{
  "presets": [
    "@babel/preset-env",
    ["@babel/preset-react", { "runtime": "automatic" }]
  ]
}
0 Answers
Related