ค้นหาเว็บไซต์

jestjs - TS-Jest และ Typescript: SyntaxError: ตัวโทเค็น 'export' ไม่คาดคิด


ฉันกำลังเจอ SyntaxError: Unexpected token 'export' บนบาง node_modules ซึ่งไม่ได้ถูกแปลง (ฉันเดา) ฉันแก้ปัญหานี้โดยการเพิ่มโมดูลเหล่านี้เข้าไปใน transformIgnorePatterns ในการตั้งค่า Jest ของฉัน แต่ดูเหมือนว่านี่จะไม่ใช่วิธีที่ถูกต้อง มีใครสามารถแชร์การตั้งค่า Jest ที่ใช้งานได้จริงหรือช่วยฉันในวิธีอื่นได้ไหม?

นี่คือการตั้งค่า Jest ของฉัน:

  "jest": {
    "preset": "ts-jest",
    "transform": {
      ".(js|ts)": "ts-jest"
    },
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!(lit-element|lit-html))"
    ],
    "testRegex": "(/__tests__/.*|\\.(unit.test|unit.spec))\\.(ts|js)$",
    "moduleNameMapper": {
      "^~/(.*)": "<rootDir>/src/$1",
      "^lodash-es$": "lodash"
    },
    "moduleFileExtensions": [
      "ts",
      "js"
    ],
    "reporters": [
      "default",
      "jest-junit"
    ],
    "coverageReporters": [
      "text",
      "cobertura"
    ]
  },

นี่คือ tsconfig.json ของฉัน:

    "compilerOptions": {
      "target": "ES6",
      "lib": [
        "dom",
        "dom.iterable",
        "esnext"
      ],
      "allowJs": true,
      "skipLibCheck": true,
      "esModuleInterop": true,
      "allowSyntheticDefaultImports": true,
      "strict": true,
      "forceConsistentCasingInFileNames": true,
      "moduleResolution": "node",
      "baseUrl": ".",
      "paths": {
        "~/*": ["src/*"]
      },
      "resolveJsonModule": true,
      "isolatedModules": true,
      "jsx": "react",
      "noEmit": false,
      "outDir": "temp",
      "sourceMap": true,
    },
    "exclude": [
        "node_modules",
        "dist"
    ]
}