如何解决Jest测试的意外令牌“导出”[已关闭]

mzsu5hc0  于 2023-08-01  发布在  Jest
关注(0)|答案(1)|浏览(118)

**已关闭。**此问题需要debugging details。它目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答这个问题。
17天前关闭
Improve this question
我正在为一个React App编写测试。在运行测试时,我得到了这个错误。Error Message
网格正在从“node_modules/gridjs/dist/index.d.ts”导出Export file
这里是我的jest.config.js

module.exports = {
  rootDir: "src",
  testEnvironment: "jsdom",
  transform: {
    "^.+\\.(j|t)sx?$": "babel-jest",
  },
  moduleNameMapper: {
    "\\.(css)$": "identity-obj-proxy",
  },
  setupFilesAfterEnv: [
    "../node_modules/@testing-library/jest-dom/dist/index.js",
  ],
};

字符串
这是我的.babelrc文件

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react",
    "@babel/preset-typescript"
  ],
  "env": {
    "test": { 
     "plugins": ["@babel/plugin-transform-modules-commonjs"]
    }
   }
}


这里是我的babel.config.json

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react",
    "@babel/preset-typescript"
  ],
  "plugins": [
    [
      "@babel/plugin-transform-runtime",
      {
        "useESModules": true,
        "regenerator": false
      }
    ]
  ],
  "env": {
    "test": {
      "presets": [
        [
          "@babel/preset-env",
          {
            "targets": "current node"
          }
        ],
        ["@babel/preset-typescript"]
      ]
    }
  }
}


下面是我的ts.config.json

{
    "compileOnSave": true,
    "compilerOptions": {
      "outDir": "./dist/",
      "rootDir": "src",
      "noImplicitAny": true,
      "module": "es6",
      "target": "es5",
      "jsx": "react",
      "allowJs": true,
      "moduleResolution": "node",
      "allowSyntheticDefaultImports": true
    },
    "include": [
      "./**/*"
    ],
    "exclude": [
      "./dist/**/*"
    ]
  }


这是我的package.json

{
  "name": "placeholder",
  "scripts": {
    "start": "webpack-dev-server --env=development",
    "build": "webpack --mode=production --env=production",
    "analyze": "webpack --mode=production --env.analyze=true  --env=production",
    "lint": "eslint src --ext js",
    "format": "prettier --write \"./**\"",
    "test": "cross-env BABEL_ENV=test jest",
    "watch-tests": "cross-env BABEL_ENV=test jest --watch",
    "coverage": "cross-env BABEL_ENV=test jest --coverage"
  },
  "devDependencies": {
    "@babel/core": "^7.21.4",
    "@babel/plugin-transform-modules-commonjs": "^7.22.5",
    "@babel/plugin-transform-runtime": "^7.21.4",
    "@babel/preset-env": "^7.21.4",
    "@babel/preset-react": "^7.18.6",
    "@babel/preset-typescript": "^7.21.4",
    "@babel/runtime": "^7.21.0",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^14.0.0",
    "@types/jest": "^29.5.0",
    "@types/systemjs": "^6.13.1",
    "babel-eslint": "^11.0.0-beta.2",
    "babel-jest": "^29.5.0",
    "concurrently": "^8.0.1",
    "cross-env": "^7.0.3",
    "eslint": "^8.38.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-config-react-important-stuff": "^3.0.0",
    "eslint-plugin-prettier": "^4.2.1",
    "husky": "^8.0.3",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^29.6.0",
    "jest-cli": "^29.5.0",
    "jest-environment-jsdom": "^29.6.0",
    "jest-fetch-mock": "^3.0.3",
    "prettier": "^2.8.7",
    "pretty-quick": "^3.1.3",
    "single-spa-react": "^5.0.1",
    "systemjs-webpack-interop": "^2.3.7",
    "webpack": "^5.78.0",
    "webpack-cli": "^5.0.1",
    "webpack-config-single-spa-react": "^4.0.4",
    "webpack-dev-server": "^4.13.2",
    "webpack-merge": "^5.8.0"
  },
  "dependencies": {
    "@emotion/react": "^11.10.6",
    "@emotion/styled": "^11.10.6",
    "@jsonforms/core": "^3.0.0-rc.0",
    "@jsonforms/material-renderers": "^3.0.0",
    "@jsonforms/react": "^3.0.0-rc.0",
    "@mui/icons-material": "^5.11.16",
    "@mui/material": "^5.11.16",
    "@mui/system": "^5.11.16",
    "@rjsf/core": "^5.5.2",
    "@testing-library/user-event": "^14.4.3",
    "@types/node": "^18.15.11",
    "@types/react": "^18.0.33",
    "@types/react-dom": "^18.0.11",
    "axios": "^1.3.5",
    "chokidar": "^3.5.3",
    "dotenv": "^16.0.3",
    "dotenv-webpack": "^8.0.1",
    "gridjs": "^6.0.6",
    "gridjs-react": "^6.0.1",
    "js-cookie": "^3.0.1",
    "material-icons-react": "^1.0.4",
    "notistack": "^3.0.1",
    "nwsapi": "^2.2.7",
    "react": "^18.2.0",
    "react-cookie": "^4.1.1",
    "react-dom": "^18.2.0",
    "react-dropdown-select": "^4.9.3",
    "react-router-dom": "^6.10.0",
    "react-scripts": "^5.0.1",
    "react-shadow-root": "^6.2.0",
    "react-use-websocket": "^4.3.1",
    "regenerator-runtime": "^0.13.11",
    "string-similarity": "^4.0.4",
    "ts-node": "^10.9.1",
    "typescript": "^4",
    "uuid": "^9.0.0"
  },
  "types": "placeholder"
}


我很确定这是一个配置问题。我已经检查了Babel,Jest和TypeScript文档,但没有找到明确的解决方案。

n6lpvg4x

n6lpvg4x1#

调整您的transformIgnorePatterns允许列表应该可以理想地解决您面临的问题:

{
  "jest": {
    "transformIgnorePatterns": [
      "node_modules/(?!@ngrx|(?!deck.gl)|ng-dynamic)"
    ]
  }
}

字符串
显然,这是因为文件没有通过TypeScript编译器转换。您可以参考排名最高的答案和评论以进一步澄清。
祝你好运,希望这能解决问题。

相关问题