Test react component with jest in typescript -> Jest encountered an unexpected token

Viewed 500

My project is written in typescript.

I am trying to test my react component with jest but when I try to run it, it's giving me this error:

   Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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.

Details:
src/components/button/button.test.tsx:20
(0, react_2.render)(<Button_1.Button label="Click me"/>);

SyntaxError: Unexpected token '<'

I tried the suggestions listed (and Google), but still couldn't resolve it.

These are my current configs:

My node version is: v12.19.1

package.json

// I've removed the none related dependencies from the list below

"devDependencies": {
    "@babel/core": "^7.15.0",
    "@babel/preset-env": "^7.15.6",
    "@babel/preset-react": "^7.14.5",
    "@babel/preset-typescript": "^7.15.0",
    "@testing-library/jest-dom": "^5.14.1",
    "@testing-library/react": "^12.1.0",
    "@types/jest": "^27.0.2",
    "@types/mocha": "^9.0.0",
    "@types/node": "^16.6.1",
    "babel-jest": "^27.2.0",
    "babel-loader": "^8.2.2",
    "jest": "^27.2.0",
    "react-test-renderer": "^17.0.2",
    "ts-jest": "^27.0.5",
    "typescript": "^4.4.3"
  }
tsconfig.json

// I've removed the none related configs from the list below

{
  "compilerOptions": {
    "types": ["jest"]
  },
  "include": ["src/**/*", "**/*.test.ts", "**/*.test.tsx"],
  "exclude": [
    "node_modules",
    "**/*.spec.ts",
    "**/*.spec.tsx"
  ]
}

babel.config.js

module.exports = {
  presets: [
    ["@babel/preset-env", { targets: { node: "current" } }],
    "@babel/preset-typescript",
  ],
};

jest.config.js

module.exports = {
  roots: ["<rootDir>/src"],
  preset: "ts-jest",
  testMatch: [
    "**/__tests__/**/*.+(ts|tsx|js)",
    "**/?(*.)+(spec|test).+(ts|tsx|js)",
  ],
  transform: {
    "^.+\\.(ts|tsx|js)$": "ts-jest",
    "\\.js$": "<rootDir>/node_modules/babel-jest",
  },
  transformIgnorePatterns: [
    "/node_modules/(?![@autofiy/autofiyable|@autofiy/property]).+\\.js$",
    "/node_modules/(?![@autofiy/autofiyable|@autofiy/property]).+\\.ts$",
    "/node_modules/(?![@autofiy/autofiyable|@autofiy/property]).+\\.tsx$",
  ],
};

Any help would greatly be appreciated.

0 Answers
Related