TypeScript项目出现意外的令牌导出-transformIgnorePattern出现问题

TypeScript项目出现意外的令牌导出-transformIgnorePattern出现问题,typescript,jestjs,Typescript,Jestjs,我有一个带有Jest测试的TypeScript项目,它工作得很好,使用ts Jest作为我的转换器。然而,当我尝试使用一个似乎是用TypeScript编写的包,但作为ES6分发时,当我的测试运行时,当它碰到新包上的“export”关键字时,我遇到了可怕的“意外令牌导出”错误 我理解为什么会发生这种情况,但即使在广泛研究了这个问题并看到其他人如何用transformIgnorePatterns解决了这个问题之后,我也不知道如何解决它(就像这篇文章和其他许多文章),但到目前为止,我还没有在这方面取得

我有一个带有Jest测试的TypeScript项目,它工作得很好,使用ts Jest作为我的转换器。然而,当我尝试使用一个似乎是用TypeScript编写的包,但作为ES6分发时,当我的测试运行时,当它碰到新包上的“export”关键字时,我遇到了可怕的“意外令牌导出”错误

我理解为什么会发生这种情况,但即使在广泛研究了这个问题并看到其他人如何用
transformIgnorePatterns
解决了这个问题之后,我也不知道如何解决它(就像这篇文章和其他许多文章),但到目前为止,我还没有在这方面取得任何进展

测试运行正常,直到我导入并引用其中一个问题库。只需添加:

import { Environment, EnvironmentType } from '@microsoft/sp-core-library';
...
const x: EnvironmentType = Environment.type;
…然后运行我的测试给我:

/Users/ac/_play/jest-transform-repro/node_modules/@microsoft/sp-core-library/lib/index.js:11
export { default as _BrowserDetection } from './BrowserDetection';
^^^^^^

SyntaxError: Unexpected token export
我已经创建了一个项目,以便在此处轻松回购我的问题:

  • 克隆
  • npm安装
  • npm试验
  • 凡事观察

  • 打开文件/src/webparts/helloWorld/components/Sample/Sample.tsx&取消对这两行的注释
  • npm试验
  • 观察失败

    我已尝试将我的
    transformIgnorePatterns
    更新为
    /../node_modules/(?!(@microsoft/sp core library))
    和其他版本,但这没有帮助。我看过关于change.bablerc=>babel.config.js的帖子,但我的项目中没有使用babel,因为它都是TypeScript

    一旦我在这一个图书馆工作,我知道我需要排除更多。。。还有很多。。。因此,理想情况下,我希望创建一些解决“@microsoft/*”软件包的东西,而不仅仅是上面列出的那个,正如您从上面repo中的默认“npm安装”中看到的那样,还有很多问题会给我带来麻烦


    想法?我不知所措:(

    这些是我为让jest在TS SPFX项目下工作所做的更改:

    tsconfig.json

    • 启用
      “allowJs”:true,
    package.json
    (添加jest配置):

    “玩笑”:{
    “moduleFileExtensions”:[
    “ts”,
    “tsx”,
    “js”,
    “json”
    ],
    “testURL”:”http://localhost",
    “转变”:{
    “^.+\.(js|ts|tsx)$”:“ts笑话”
    },
    “transformIgnorePatterns”:[
    “节点模块/(?!(@microsoft/sp dialog |@microsoft/office ui fabric react bundle |@microsoft/sp diagnostics |@microsoft/sp核心库|@microsoft/sp http |@microsoft/sp页面上下文|@microsoft/sp动态数据|@pnp/sp |@pnp/common |@pnp/odata pnp))”
    ],
    “测试匹配”:[
    “***/src/***/.test.+(ts|tsx|js)”,
    “***/\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
    ],
    “moduleNameMapper”:{
    “\\(css |减去| scss | sass)$”:“身份obj代理”,
    “^resx strings/en us.json”:“/node_modules/@microsoft/sp core library/lib/resx strings/en us.json”
    },
    }
    
    附加包:

    • “ts笑话”:“^24.0.2”,
    • “jest”:“^26.6.3”,“jest junit”:“^12.0.0”,
    • “@types/jest”:“^26.0.15”,
    • “身份obj代理”:“^3.0.0”,
    另一方面,我使用的不是
    ,而是
    反应测试库

        "jest": {    
        "moduleFileExtensions": [
          "ts",
          "tsx",
          "js",
          "json"
        ],
    
        "testURL": "http://localhost",
        "transform": {
          "^.+\\.(js|ts|tsx)$": "ts-jest"
        },
        "transformIgnorePatterns": [
          "node_modules/(?!(@microsoft/sp-dialog|@microsoft/office-ui-fabric-react-bundle|@microsoft/sp-diagnostics|@microsoft/sp-core-library|@microsoft/sp-http|@microsoft/sp-page-context|@microsoft/sp-dynamic-data|@pnp/sp|@pnp/common|@pnp/odata|@pnp))"
        ],
        "testMatch": [
          "**/src/**/*.test.+(ts|tsx|js)",
          "**/__tests__/**/*.(spec|test).+(tsx|ts|js)?(x)"
        ],
        "moduleNameMapper": {
          "\\.(css|less|scss|sass)$": "identity-obj-proxy",
          "^resx-strings/en-us.json": "<rootDir>/node_modules/@microsoft/sp-core-library/lib/resx-strings/en-us.json"
        },
      }