首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SyntaxError:使用Jest时意外标记的“导出”(来自“react传单”)

SyntaxError:使用Jest时意外标记的“导出”(来自“react传单”)
EN

Stack Overflow用户
提问于 2022-08-26 17:21:44
回答 1查看 782关注 0票数 2

我不知从哪里得到了这个错误。

在我使用它的时候,代码可以很好地工作,但是在使用Jest进行测试时,会弹出这些代码。......................................................................................................................................................................................................................................................................................................................................................................................................................

详细信息:

代码语言:javascript
复制
    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 |
         | ^

这是我的jest配置

代码语言:javascript
复制
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

代码语言:javascript
复制
  "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 & config

代码语言:javascript
复制
{
    "presets": ["@babel/preset-env", "@babel/preset-react"]
}
代码语言:javascript
复制
module.exports = {
  presets: [
    [
      '@parcel/babel-preset-env',
      {
        targets: {
          esmodules: true,
        },
      },
    ],
  ],
};

是不是有什么东西可能导致了这件事,还是我遗漏了别的什么?

EN

回答 1

Stack Overflow用户

发布于 2022-10-03 19:01:58

最终的解决办法是

代码语言:javascript
复制
const esModules = ['@react-leaflet', 'react-leaflet'].join('|');

在jest.config.js之上

在配置中

代码语言:javascript
复制
  transformIgnorePatterns: [`/node_modules/(?!${esModules})`],
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73504569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档