admin管理员组文章数量:1291786
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. I'm getting the dreaded SyntaxError: Unexpected token export
when trying to run jest.
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 ing 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.
How can I get jest to properly handle the files imported from @agm/core
?
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. I'm getting the dreaded SyntaxError: Unexpected token export
when trying to run jest.
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 ing 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.
How can I get jest to properly handle the files imported from @agm/core
?
3 Answers
Reset to default 4There 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.
yarn add --dev babel-preset-env
You should already have 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:
{
"presets": ["babel-preset-env"]
}
This may work for other libraries that need a babel transformation as-installed.
In case you're experiencing this for
/node_modules/@nestjs/mon/node_modules/uuid/dist/esm-browser/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
^^^^^^
the fix is to add
moduleNameMapper: {
'^uuid$': require.resolve('uuid'),
}
to the jest configuration, e.g. to jest.config.js
.
Thanks to contributors at https://github./nestjs/nest/issues/9930.
i would like to add that for babel 7+ i guess, instead of .babelrc use babel.config.js:
module.exports = {
presets: ['babel-preset-env'],
}
本文标签: javascriptUnexpected token export with jestStack Overflow
版权声明:本文标题:javascript - Unexpected token export with jest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741540267a2384281.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论