Jest给出了一个错误:"SyntaxError:意外的令牌导出"

Don*_*n P 9 jestjs

我正在使用Jest来测试我的React应用程序.

最近,我将DeckGL添加到了我的应用程序中.我的测试因此错误而失败:

Test suite failed to run

/my_project/node_modules/deck.gl/src/react/index.js:21
export {default as DeckGL} from './deckgl';
^^^^^^

SyntaxError: Unexpected token export

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:318:17)
  at Object.<anonymous> (node_modules/deck.gl/dist/react/deckgl.js:9:14)
  at Object.<anonymous> (node_modules/deck.gl/dist/react/index.js:7:15)
Run Code Online (Sandbox Code Playgroud)

这似乎是Jest在运行它的测试之前转换节点模块的问题.

这是我的.babelrc:

{
  "presets": ["react", "es2015", "stage-1"]
}
Run Code Online (Sandbox Code Playgroud)

这是我的开玩笑设置:

"jest": {
    "testURL": "http://localhost",
    "setupFiles": [
      "./test/jestsetup.js"
    ],
    "snapshotSerializers": [
      "<rootDir>/node_modules/enzyme-to-json/serializer"
    ],
    "moduleDirectories": [
      "node_modules",
      "/src"
    ],
    "moduleNameMapper": {
      "\\.(css|scss)$": "<rootDir>/test/EmptyModule.js"
    }
  },
Run Code Online (Sandbox Code Playgroud)

我似乎有正确的事情需要改造export {default as DeckGL }.所以任何想法都会出错?

Ria*_*ini 12

这意味着,文件不是通过TypeScript编译器转换的,例如因为它是带有TS语法的JS文件,或者它作为未编译的源文件发布到npm.这是你可以做的.

调整transformIgnorePatterns白名单:

{
  "jest": {
    "transformIgnorePatterns": [
      "node_modules/(?!@ngrx|(?!deck.gl)|ng-dynamic)"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

默认情况下,Jest不会转换node_modules,因为它们应该是有效的JavaScript文件.但是,图书馆作者认为你会编译他们的资源.所以你必须明确告诉Jest.上面的片段意味着@ngrx,deck和ng-dynamic将被转换,即使它们是node_modules.

  • 我该把这个放在哪里?哪个文件? (22认同)
  • @EladKatz 我对此很陌生,但我认为这个块适用于 package.json,如果没有“jest”包装器,您可以将其放入 jest.config.js 中。 (12认同)
  • 正则表达式中嵌套的负向先行 `(?!...(?!...))` 的原因是什么? (9认同)
  • 可能只是想像这样简化正则表达式:`node_modules\/(?!(@ngrx|deck.gl|ng-dynamic))` (9认同)
  • @EladKatz 在 `jest.config.js` 文件中。 (3认同)

Jim*_*ala 12

这是因为Node.js无法处理ES6模块.

因此,您应该将模块转换为CommonJS.

如果你使用Babel 7 =>

安装 npm install --save-dev @babel/plugin-transform-modules-commonjs

并且仅用于测试用例添加到.babelrc,Jest自动给出NODE_ENV = test全局变量.

"env": {
    "test": {
      "plugins": ["@babel/plugin-transform-modules-commonjs"]
    }
}
Run Code Online (Sandbox Code Playgroud)

或者如果你使用Babel 6 =>

npm install --save-dev babel-plugin-transform-es2015-modules-commonjs

到.babelrc

"env": {
    "test": {
      "plugins": ["transform-es2015-modules-commonjs"]
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这有效!但是,请确保运行 `jest --clearCache` 在它开始工作之前我必须这样做。 (2认同)
  • 我在哪里添加: "env": { "test": { "plugins": ["@babel/plugin-transform-modules-commonjs"] } } (2认同)
  • @joeCarpenter 到你的 `.babelrc` 或你可能使用的其他 babel 配置文件,例如 `babel.config.js`。如果您还没有,您可以在项目根目录中创建名为“.babelrc”的新文件,并将其添加到根“{ }”标签下。 (2认同)
  • 必须将此答案与 @Ria Anggraini 的答案结合起来,才能在运行测试时正常工作。 (2认同)

Ell*_*ice 12

尽管上面的答案中提到了该页面上的解决方法#2 ,但该页面上的解决方法#1 为我解决了这个问题,因此它们也可能有效。

“在配置中指定对应包的commonjs版本的入口moduleNameMapper

jest.config.js

    moduleNameMapper: {
        "^uuid$": require.resolve("uuid"), 
        "^jsonpath-plus$": require.resolve("jsonpath-plus") 
...
Run Code Online (Sandbox Code Playgroud)


小智 11

就我而言,我在文件 package.json 中使用此配置:

"jest": {
    "transformIgnorePatterns": [
      "!node_modules/"
    ]
  }
Run Code Online (Sandbox Code Playgroud)


luk*_*eke 10

Jest 默认不会编译目录中的node_modules文件。

transformIgnorePatterns [数组]

默认值:["/node_modules/"]

转换前与所有源文件路径匹配的正则表达式模式字符串数组。如果测试路径匹配任何模式,则不会被转换。默认:["/node_modules/"]

DeckGL 似乎在 ES6 中,为了让 jest 能够阅读它,您还需要编译它。
为此,只需在transformignorePatterns

"transformIgnorePatterns": ["/node_modules/(?!deck\.gl)"]
Run Code Online (Sandbox Code Playgroud)

https://facebook.github.io/jest/docs/en/configuration.html#transformignorepatterns-array-string


Pie*_*lho 6

我在 monorepo 上遇到了这个问题。根目录中的一个包node_modules破坏了我的测试。我通过将本地.babelrc文件更改为babel.config.js. 说明:https ://github.com/facebook/jest/issues/6053#issuecomment-383632515


Ram*_*Ram 5

而且,如果您使用的是“ create-react-app”,则不允许您通过package.json中的 Jest属性指定“ transformIgnorePatterns”

按照这个https://github.com/facebook/create-react-app/issues/2537#issuecomment-390341713

您可以在package.json中使用以下CLI 进行覆盖,并且可以使用:

"scripts": { "test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!your-module-name)/\"", },

  • CRA [3.0.2 添加](https://github.com/facebook/create-react-app/pull/6055#event-2419503373) 对 package.json 中 Jest `transformIgnorePatterns` 的支持。 (2认同)