【问题标题】:Facing Jest encountered an unexpected token while using Require with JEST面对 Jest 在将 Require 与 JEST 一起使用时遇到了意外的令牌
【发布时间】:2022-02-11 02:15:30
【问题描述】:

我有这样的问题:

Jest 遇到了意外的令牌

我没有为我的样板找到正确的解决方案。我在这里检查了 JEST 的文档或一些代码,但我所做的事情不起作用。你能帮我解决这个问题吗?我在下面添加了知识。

我的 package.json:

{
  "jest": {
    "testMatch": [
      "<rootDir>/src/**/*.test.{js,jsx,ts,tsx}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}"
    ],
    "testEnvironment": "jsdom"
  },
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "@babel/core": "^7.17.2",
    "@babel/preset-env": "^7.16.11",
    "@babel/preset-react": "^7.10.4",
    "@babel/preset-typescript": "^7.10.4",
    "@svgr/webpack": "^5.5.0",
    "@testing-library/react": "^12.1.2",
    "babel-loader": "^8.2.3",
    "cross-env": "^7.0.2",
    "html-webpack-plugin": "^4.4.1",
    "mini-css-extract-plugin": "^0.11.1",
    "node-sass": "^4.14.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "typescript": "^4.0.2",
  },
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.2",
    "babel-jest": "^27.5.1",
    "dotenv": "^8.2.0",
    "dotenv-webpack": "^6.0.2",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^27.5.1",
    "jest-config": "^27.5.1",
    "react-dotenv": "^0.1.3",
    "ts-jest": "^27.1.3"
  }
}

我的 jest.config.js:

module.exports = {
  preset: 'ts-jest',
  setupFilesAfterEnv: ["@testing-library/jest-dom/extend-expect"],
  testEnvironment: "jsdom",
  verbose: true,
  moduleNameMapper: {
    "\\.(css|less|scss|sss|styl)$": "<rootDir>/node_modules/identity-obj-proxy"
  }
};

.babelrc:

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react",
    "@babel/preset-typescript"
  ]
}

错误图片:

【问题讨论】:

    标签: reactjs unit-testing jestjs babel-jest


    【解决方案1】:

    不要将 require 与 import 混合使用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 2019-10-07
      • 2021-04-19
      • 2021-12-07
      相关资源
      最近更新 更多