-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SyntaxError: Unexpected token import #64
Comments
Hard to tell without a repro, or at least more detailed error (with stack trace) |
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:
|
Have you tried running jest with |
I have tried --no-cache yes, I have a script in my package.json file All the line numbers in the error appear to point to the end of the import statements in those files, so it appears it is unable to import anything from node_modules? The full section about jest in my package.json file is as follows:
|
It should be |
Sorry, that is what i meant, just a mistake in my typing. |
All the errors are pointing to lines in files where there are imports, such as: Am I missing anything that allows Jest to access the node_modules folder? It currently appears like it just can't find it or access it. I am just guessing though. |
By default Jest doesn't transpile code in node_modules, hence the whitelist in
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:
|
Ok that makes sense, so I have now tried with all node_modules as:
and the error stays exactly the same (very odd), but changing the list values to be:
This now gives me a completely new error:
So, following the same pattern, I have added
|
You shouldn't add typescript to whitelist, the error you get is not SyntaxError. Sorry, but no idea right now, what may be the cause right now. You may want to search for this error in |
OK, I have tried to experiment with adding/removing packages from the whitelist, but ultimately now I am stuck on the
|
@thymikee I have tried the suggested solution in #66 and unfortunately I am still getting the same Unexpected token error. It only happens when I am importing a package in the .spec file and attempt to use something from that package. The package is very very basic at the moment, and is a package I created myself. |
I've made a demo repo where the error is occurring: https://github.com/kidd3/SL |
Thanks, I'll get to it when I have some free time, because I'm pretty busy at the moment :< |
What is your babel config? Also what is your jest config? Actulally you don't need babel. Jest already have babel-jest out of the box. You can use it. Why you use babel? |
Thanks @thymikee, I appreciate it, sorry you are busy. @sharikovvladislav I wasn't using babel, however I was attempting to see if the fix suggested on #66 to use a .babelrc file and updating my jest config inside the package.json file to include: |
Can you show your jest config? We can try to solve your problem as my was sovled. If it is not similar, I don't think I can help :( So lets see. |
Running jest --debug gives me this:
|
Thanks @sharikovvladislav, is the above information what you need? |
Can you provide full output where you see the error? |
Sure, the error is: ● Test suite failed to run
/Users/user/Documents/Jest test/SL/node_modules/jest-test/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { Component, Injectable, NgModule } from '@angular/core';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
at Object.<anonymous> (src/services/register.service.spec.ts:3:17)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 2.954s
Ran all test suites. |
I have a demo repo here as well if it is helpful, as it has the error occurring: https://github.com/kidd3/SL |
@kidd3 ok sry my bad. I forget. Yes you need .babelrc for setting preset. Here is config:
and here is babelrc contents:
|
Here is PR. It solves the problem: https://github.com/kidd3/SL/pull/4 |
I also pushed coverage, accidently removed it. |
@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? |
Nope. Main is adding |
ah damn, I'm a fool! I had a .babelrc file but I had put it inside the /src folder when it needed to be in the root folder! Thank you @sharikovvladislav for your fresh eyes to see this and find it, I appreciate it very much! |
Nope. Thank you to @DorianGrey. He perfectly explained everything there: #66 (comment) |
Be sure that I figured it out the hard way ;) Technically, it should be sufficient to just use a babel config like this:
This is included in the |
Why is babel necessary, shouldn't the tsc be able to compile to es5? There is no mention of it in the docs. |
@moneydance it turns out that tsc does not compile to es5 some of files. We already realized that with @thymikee and I am going to make a PR to fix this. @thymikee it turns out that we should change not only docs, but config generated by preset, shouldn't we? |
Hey thanks for the quick response! Okay I will try adding .babelrc and see if it fixes my issues. |
@sharikovvladislav yea, that's probably a right thing to do, because compiling js through |
Closing this issue, but let's keep the discussion going |
Also having same issue |
I'm having the same problem and the steps described didn't help |
Hard to help without any repro. I guess, you already checked this one #66 (comment)? |
Yup... I get |
Try to narrow it down to simples case and go from there. This error tells me nothing unfortunately. |
None of this works of angular 6 ... Is there any proper solution for angular 6? |
@DamengRandom what is your test setup and what are the test cases which produce the error ? |
Still the same problem here |
@mo-norant if you could provide a reproduction, preferably with a minimal project, we can look into it. |
Hi @mo-norant , this issue has been answered in #185 . It is related to your 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 |
If anyone come across that error take a look into the PR above ☝️ |
I upgraded office-ui-fabric-react package from 5.124.0 to 6.164.0 and my tests are failing with the following error: ● Test suite failed to run
Please see the package.json file below: Can anyone please help me to resolve this? |
@xyz92 I don't think you should test a react-library with an Angular Jest preset. Seems like your transformer does not like es6, make sure you set es5 as compilation target for TyepScript. |
in addition, regarding to test react-library with typescript and jest, please refer to |
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:
And my tsconfig.spec.json file is as follows:
Any help would be appreciated
The text was updated successfully, but these errors were encountered: