简体   繁体   English

玩笑意外地导出令牌

[英]Unexpected token export with jest

I've seen some other answers and GitHub issues that describe this, but I haven't been able to find a solution in my case. 我已经看到其他一些答案和GitHub问题来描述这一点,但是我无法找到解决方案。 I'm getting the dreaded SyntaxError: Unexpected token export when trying to run jest. 尝试运行玩笑时,我得到了可怕的SyntaxError: Unexpected token export

project/node_modules/@agm/core/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './directives';

The code where these tests are being run works okay. 运行这些测试的代码可以正常工作。 The error seems to be coming from within jest itself. 错误似乎来自玩笑本身。 It seems like jest is transforming the file, but I could be wrong. 似乎开玩笑正在转换文件,但我可能是错的。

My jest configuration is 我开玩笑的配置是

"jest": {
  "preset": "jest-preset-angular",
  "setupTestFrameworkScriptFile": "<rootDir>/setup-jest.ts",
  "transformIgnorePatterns": [
    "node_modules"
  ]
}

I've tried updating the transformIgnorePatterns to "<rootDir>/node_modules/(?!@agm)" and "node_modules/(?!@agm)" , "node_modules/(?!@agm/core)" , but none of those seem to make any difference. 我尝试将transformIgnorePatterns更新为"<rootDir>/node_modules/(?!@agm)""node_modules/(?!@agm)""node_modules/(?!@agm/core)" ,但是都没有这些似乎没有什么不同。

How can I get jest to properly handle the files imported from @agm/core ? 我怎样才能开玩笑地正确处理从@agm/core导入的文件?

There are several changes that I needed to get this to work. 为了使它起作用,我需要进行一些更改。 This should fix the issue when using @agm/core for an Angular 6 app is being used with jest. 当使用@agm/core进行Angular 6应用时,这应该可以解决该问题。

yarn add --dev babel-preset-env

You should already have babel-jest . 您应该已经有了babel-jest

Then update your jest configuration: 然后更新您的笑话配置:

"transform": {
  "^.+\\.js": "<rootDir>/node_modules/babel-jest"
},
"transformIgnorePatterns": [
  "<rootDir>/node_modules/(?!@agm)"
]

You will also need to add a .babelrc to use if you don't have one: 如果您还没有一个,则还需要添加一个.babelrc来使用:

{
  "presets": ["babel-preset-env"]
}

This may work for other libraries that need a babel transformation as-installed. 这可能适用于需要安装babel转换的其他库。

i would like to add that for babel 7+ i guess, instead of .babelrc use babel.config.js : 我想为babel 7+添加它,而不是.babelrc,请使用babel.config.js

module.exports = {
    presets: ['babel-preset-env'],
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM