jest-preset-angular: SyntaxError: Unexpected token import
I have a similar issue to the one posted in #4, however I have tried the suggested fixes and none of them are resolving the issue for me.
I am importing a package of my own creation into an Angular 2+ project, I can build the project and run it perfectly, but running tests in Jest with Jest-Preset-Angular throws the error:
SyntaxError: Unexpected token import
I’ve tried adding my package to a whitelist such as:
"transformIgnorePatterns": [
"node_modules/(?!@ngrx|service-layer)"
]
And my tsconfig.spec.json file is as follows:
{
"extends": "./tsconfig.es5.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"module": "commonjs",
"target": "es6",
"baseUrl": "",
"types": [
"jest",
"node"
],
"allowJs": true
},
"files": [
"**/*.spec.ts"
]
}
Any help would be appreciated
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 51 (22 by maintainers)
Commits related to this issue
- docs: do not use .babelrc file but rather babel.config.js Related to thymikee/jest-preset-angular#64 — committed to maxime1992/jest-preset-angular by maxime1992 5 years ago
- docs: do not use .babelrc file but rather babel.config.js (#231) Related to thymikee/jest-preset-angular#64 — committed to thymikee/jest-preset-angular by maxime1992 5 years ago
Be sure that I figured it out the hard way 😉 I’ve evaluated the usage of
jesta while ago in my angular template and faced a similar problem withlodash-es: https://github.com/DorianGrey/ng-webpack-template/commit/dce61c91cdfbfa82040970307b01025ac00b1914Technically, it should be sufficient to just use a babel config like this:
This is included in the
envpreset, in addition to several other plugins and a basic config that can deal with way more cases than the “simple” one I’ve had withlodash-es, which is why I suggested it in favor of the single plugin version.@kidd3 ok sry my bad. I forget. Yes you need .babelrc for setting preset.
Here is config:
and here is babelrc contents:
It should be
npm run test -- --no-cacheNone of this works of angular 6 … Is there any proper solution for angular 6?
By default Jest doesn’t transpile code in node_modules, hence the whitelist in
transformIgnorePatterns. You can set it to:if you want to transpile all node_modules (not recommended though). It’s advised to add untranspiled node modules to this list, so you’d end up with something like:
Hi @mo-norant , this issue has been answered in #185 . It is related to your
tsconfig.spec.json. You need to addmodule: commonjsand then runjest --clearCache. After that you can run your test again.We also have an example app with base configuration in our example folder or you can take a look at my sample repo at https://github.com/ahnpnl/jest-angular
Nope. Thank you to @DorianGrey. He perfectly explained everything there: https://github.com/thymikee/jest-preset-angular/issues/66#issuecomment-323696947
Nope. Main is adding
.babelrc. http://prntscr.com/gcxyu4@sharikovvladislav the only thing I can see that has changed is:
becomes:
For me this 1 change of adding the extra brackets isn’t fixing the issue, did I miss something?
Sorry, the project I am working on is difficult to share but I will see if I can recreate a repo. The full error i get is:
in addition, regarding to test react-library with typescript and jest, please refer to
ts-jest