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 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.
How can I get jest to properly handle the files imported from @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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With