Babel.js 语法错误:使用Jest时出现意外标记“export”(来自“react-leaflet”)

r1wp621o  于 2022-12-08  发布在  Babel
关注(0)|答案(1)|浏览(229)

我得到这个错误不知从哪里来。
代码在我使用时可以正常工作,但在使用 Jest 进行测试期间,出现以下情况:...............................................................................................................................................................................................................................................................................................
详细信息:

C:\node_modules\react-leaflet\lib\index.js
:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { us
eMap, useMapEvent, useMapEvents } from './hooks.js';
                                                                                      ^^^^^^     

    SyntaxError: Unexpected token 'export'

       9 | } from 'react-leaflet';
      10 | import { private } from 'private';
    > 11 |
         | ^

这是我的玩笑配置

module.exports = {
  moduleNameMapper: {
    // Resolve .css and similar files to identity-obj-proxy instead.
    '.+\\.(css|styl|less|sass|scss)$': 'identity-obj-proxy',
    // Resolve .jpg and similar files to __mocks__/file-mock.js
    '.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
      '<rootDir>/__mocks__/file-mock.js',
  },

  clearMocks: true,

  coverageDirectory: 'coverage',
  // In jest.config.js add (if you haven't already)
  setupFilesAfterEnv: ['./src/setupTests.js'],

  coverageProvider: 'v8',

  globals: {
    'ts-jest': {
      isolatedModules: true,
    },
  },

  rootDir: '.',
  moduleDirectories: ['node_modules', 'src', 'utils'],
  testEnvironment: 'jsdom',
  testTimeout: 20000,

  roots: ['<rootDir>/src'],
  testMatch: [
    '**/__tests__/**/*.+(ts|tsx|js)',
    '**/?(*.)+(spec|test).+(ts|tsx|js)',
  ],
  transform: {
    '^.+\\.js?$': 'babel-jest',
    '^.+\\.(t|j)sx?$': ['@swc/jest'],
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  transformIgnorePatterns: [`/node_modules/(?!${esModules}')`],
};

TsConfig

"include": [
    "src/**/*.ts",
    "src/**/*.tsx",
  ],
  "exclude": [
    "node_modules",
    "dist"
  ],
  "compilerOptions": {
    "target": "es2015",
    "module": "esnext",
    "declaration": true,
    "lib": [
      "es2021",
      "es2017",
      "es2015",
      "dom",
      "ESNext"
    ],
 
    "allowJs": false,
    "jsx": "react-jsx",
    "sourceMap": true,
    "outDir": "dist",
    "rootDir": "src",
    "removeComments": true,
    "strict": true,
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "isolatedModules": true,
    "noEmit": true,
    "baseUrl": "src",
    "paths": {
      "test-utils": [
        "./utils/test-utils"
      ]
    }
  }
}

.Bablerc和配置

第一个
是不是有什么东西导致了这个还是我遗漏了什么?

e5nqia27

e5nqia271#

解决方案最终是

const esModules = ['@react-leaflet', 'react-leaflet'].join('|');

在jest.config.js之上
并在配置中

transformIgnorePatterns: [`/node_modules/(?!${esModules})`],

相关问题