Jest SyntaxError:从节点模块“导出”意外的标记

MR *_*R J 6 testing node.js reactjs jestjs

我和 Jest 一直有这个问题。Jest 抛出一个错误,指向 node_modules 内的依赖项。可能需要配置 jest 以支持此语法,因为它无法解析它。但我不确定配置是什么。

我尝试在 jest.config.ts 文件中添加transformIgnorePatterns,但没有成功,

    "node_modules/(?!(geotiff)/)"
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

Details:

    /Users/Documents/Project/node_modules/quick-lru/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export default class QuickLRU extends Map {
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-cli/node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (node_modules/geotiff/dist-module/source/blockedsource.js:1:1)
Run Code Online (Sandbox Code Playgroud)

这是 jest.config.ts

// jest.config.ts
import type {Config} from "@jest/types";

const config : Config.InitialOptions = {
  "collectCoverageFrom": [
    "src/**/*.{js,jsx,ts,tsx}",
    "!**/*.d.ts"
  ],
  "setupFiles": [
    "<rootDir>/config/polyfills.js"
  ],
  "setupFilesAfterEnv": [
    "<rootDir>/src/setupTests.ts"
  ],
  "testMatch": [
    "<rootDir>/src/**/*.(test).{js,jsx,ts,tsx}",
    "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}"
  ],
  "testEnvironment": "node",
  "testEnvironmentOptions": {
    "url" :  "http://localhost"
  },
  "transform": {
    "^.+\\.(js|jsx|mjs)$": "<rootDir>/node_modules/babel-jest",
    "^.+\\.tsx?$": "<rootDir>/config/jest/typescriptTransform.js",
    "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
  },
  "transformIgnorePatterns": [
    "node_modules/(?!(geotiff)/)"
  ],
  "moduleFileExtensions": [
    "web.ts",
    "ts",
    "web.tsx",
    "tsx",
    "web.js",
    "js",
    "web.jsx",
    "jsx",
    "json",
    "node",
    "mjs"
  ],
  "globals": {
    "ts-jest": {
      "tsconfig": "./tsconfig.test.json"
    }
  },
  "modulePaths": [
    "<rootDir>/src/"
  ]
};
Run Code Online (Sandbox Code Playgroud)

这是 package.json 文件

{
  "name": "Project",
  "version": "1",
  "private": true,
  "homepage": ".",
  "files": [
    "build/**/*"
  ],
  "engines": {
    "node": ">=16.14.0",
    "npm": ">=8.11.0"
  },
  "repository": {
    "type": "git",
    "url": "ssh://Project},
  "resolutions": {
    "@types/react": "17.0.2",
    "@types/react-dom": "17.0.2"
  },
  "scripts": {
    "build-css": "sass --style=compressed -I ./src -I ./node_modules src/:src/",
    "watch-css": "npm run build-css && sass -I ./src -I ./node_modules src/:/src --watch",
    "start-js": "node scripts/start.js",
    "start": "npm-run-all -p watch-css start-js",
    "debug": "npm-run-all -p --inspect=9227 watch-css start-js",
    "prebuild": "cross-env CI=true npm run test --watchAll=false",
    "build": "npm run build-css && node scripts/build.js",
    "postbuild": "node scripts/zip.js",
    "build-test": "npm run build-css node scripts/build.js",
    "serve-build": "npm run serve -s build",
    "test": "node scripts/test.js --env=jsdom",
    "prepublishOnly": "npm run build",
    "preversion": "CI=true npm run test",
    "postinstall": "patch-package",
    "lint": "./node_modules/.bin/ESLint -c .eslintrc.js -o ./lint.log"
  },
  "devDependencies": {
    "@babel/cli": "^7.18.6",
    "@babel/core": "^7.18.6",
    "@babel/preset-env": "^7.18.6",
    "@babel/preset-react": "^7.18.6",
    "@babel/preset-typescript": "^7.18.6",
    "@babel/runtime": "^7.18.6",
    "@svgr/webpack": "^6.2.1",
    "@turf/turf": "^6.4.0",
    "@types/classnames": "^2.3.1",
    "@types/enzyme": "^3.10.12",
    "@types/jest": "^28.1.3",
    "@types/node": "^18.0.0",
    "@types/react": "^17.0.39",
    "@types/react-autocomplete": "^1.8.6",
    "@types/react-autosuggest": "^10.1.5",
    "@types/react-beautiful-dnd": "^13.1.2",
    "@types/react-highlight": "^0.12.5",
    "@types/react-router-dom": "^5.3.3",
    "@types/uuid": "^8.3.4",
    "@typescript-eslint/parser": "^5.30.0",
    "@wojtekmaj/enzyme-adapter-react-17": "^0.6.7",
    "adm-zip": "^0.5.9",
    "autoprefixer": "10.4.7",
    "awesome-typescript-loader": "^5.2.1",
    "axios": "^0.27.2",
    "babel-core": "7.0.0-bridge.0",
    "babel-jest": "^28.1.1",
    "babel-loader": "^8.2.2",
    "babel-preset-react-app": "10.0.1",
    "case-sensitive-paths-webpack-plugin": "^2.4.0",
    "chalk": "5.0.1",
    "classnames": "~2.2.6",
    "core-js": "^3.6.5",
    "cross-env": "^5.2.1",
    "css-loader": "^6.7.1",
    "dotenv": "16.0.1",
    "enzyme": "^3.11.0",
    "eslint": "^8.18.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-jsdoc": "^39.3.3",
    "eslint-plugin-prefer-arrow": "^1.2.3",
    "eslint-plugin-react": "^7.22.0",
    "eslint-webpack-plugin": "^3.2.0",
    "file-loader": "^6.1.1",
    "fork-ts-checker-webpack-plugin": "^7.2.11",
    "fs-extra": "10.1.0",
    "geotiff": "^2.0.5",
    "html-webpack-injector": "^1.1.4",
    "html-webpack-plugin": "^5.5.0",
    "jest": "^28.1.1",
    "jest-canvas-mock": "^2.4.0",
    "jest-environment-jsdom": "^28.1.1",
    "joi-browser": "^13.4.0",
    "mini-css-extract-plugin": "^2.6.1",
    "neo-async": "^2.6.2",
    "npm-run-all": "^4.1.3",
    "object-assign": "^4.1.1",
    "ol": "~6.14.1",
    "ol-ext": "^3.2.26",
    "patch-package": "^6.2.2",
    "proj4": "^2.5.0",
    "promise": "^8.0.1",
    "raf": "^3.4.0",
    "react": "^17.0.2",
    "react-app-polyfill": "^3.0.0",
    "react-autocomplete": "^1.8.1",
    "react-autosuggest": "^10.1.0",
    "react-beautiful-dnd": "^13.1.0",
    "react-dev-utils": "^12.0.0",
    "react-docgen-typescript-webpack-plugin": "^1.1.0",
    "react-dom": "^17.0.2",
    "react-highlight": "^0.14.0",
    "react-redux": "^8.0.2",
    "react-scripts": "^5.0.0",
    "redux": "^4.0.5",
    "redux-mock-store": "^1.5.4",
    "redux-thunk": "^2.3.0",
    "resolve": "^1.6.0",
    "sass": "^1.49.9",
    "serve": "^13.0.2",
    "sneakpeek-cli": "^0.2.1",
    "source-map-loader": "^4.0.0",
    "swiper": "^6.8.4",
    "ts-jest": "^28.0.5",
    "ts-loader": "^9.3.1",
    "ts-node": "^10.8.1",
    "tsconfig-paths-webpack-plugin": "^3.5.1",
    "typescript": "^4.7.4",
    "url-loader": "^4.1.1",
    "uuid": "^7.0.3",
    "webpack": "~5.73.0",
    "webpack-dev-server": "^4.9.2",
    "webpack-manifest-plugin": "^5.0.0"
  },
}

Run Code Online (Sandbox Code Playgroud)