-
Notifications
You must be signed in to change notification settings - Fork 492
jest tests failing with "Unexpected token export" when using absolute imports #122
Comments
This "recommendation" was never made - it's an assumption of the user that closed the issue without waiting for a response. And it's just a hack that isn't proven to work in general. The error message you get indicates that the file was never processed by the typescript compiler, and thus still contains the However... this is not as easy is it seems to be, at least considering the technical circumstances. These path mappings have to be added to:
And that's where the problem with the default CRA setup begins - without ejecting the project, you won't have access to the other configs to add an entry to While this is possible ... I wouldn't recommend it. It will force you to maintain three definition points for the path mappings, each of them with a different syntax (TS: Glob, Webpack: Plain path, Jest: Regexp). I'd recommend to deleted your |
Thanks for the detailed response. To clarify, the recommendation to use node_modules was originally made by Dan Abramov (CRA author) in the main CRA project facebook/create-react-app#1065, so doing that way is a semi-official recommendation, at least for the main project. And src/node_modules seems to work pretty well, especially with tooling. I have two mid-sized projects that work fine with this approach, until I ran into this jest issue with enums. The fact that introducing the enums causes the issue, gives me some kind of hope this might be a fixable bug in this project. The tsc compiler is obviously processing the I'll experiment with ejection config, but it seems like a real shame if |
And both solutions are not ideal, technically - although they work fine in quite a lot of cases, these are more like workarounds. Regarding the enum issue - I've found some issues in the |
Ok, got it - the problem is caused by this line:
When changing it to
(like in the original repo) everything works fine. A bit more detailed: As I considered when reading the original error message, the Regarding the change I've tested above - that tends to the same direction. If the exclusion is changed to allow @wmonk , what do you think ? |
Thank you for the detailed investigation! A few thoughts while waiting for @wmonk to chime in. (1) I agree that the (2) If we do want to support (3) Or perhaps it would be less hacky to specifically designate a folder like @jaredpalmer and I created a fork here (https://github.com/palmerhq/create-react-app-typescript/tree/palmerhq) based on your suggestions that sets up (4) If the project POV is that absolute imports are simply unsupported (which is totally your right), loads of |
Just a few notes:
I.e.: (2) would be a quick solution, but would enforce the |
Thanks both for this discussion! I've read through most of it and am happy to make this work for people, as it seems to be something that comes up relatively often in our issues. Can you give your opinion on the most effective, but also least likely to impact users who are not interested in this feature way to achieve this? |
The most effective - though not technically likely - solution would be to adjust the regexp here to
so that it only covers the root's The alternative to define a particular folder for absolute imports is not only a more technically advanced topic (since it's required to support both regular and ejected mode), it is also the more opinionated one, as it enforces a particular folder to refer to. Would be the less hacky approach, however. If correctly set up, it should not break anything. It is already illustrated in the fork mentioned above, though it has to checked if it works properly with and without ejection. Personally, I'd favor the second approach, since I don't like the hacky nature of the first one. |
I would also vote for the second approach, if we are okay with the project being a little opinionated. I've seen a few other flaky behaviors relating to the use of I personally use What would be the next step to move this forward? Should we test the fork and submit a PR? |
It's a swamp of unsupported features in create-react-scripts-ts wmonk/create-react-app-typescript#122 (comment) This reverts commit 96f9b8e.
i'd come across this issue when wanting to use absolute paths in my code for helper methods, such as in instead of having to do i achieved this in 4 steps:
To get tests working (jest), in
comments, questions and suggestions welcome to improve this. otherwise, i hope this helps for anybody else running into this issue |
@imzaid If I do that I get the following error when running
|
hi @bjarkehs, sorry about that. it seems i missed updating the test portion in
i've updated my previous post to reflect that. |
I am currently trying to get absolute imports working with |
i'm not sure that's wise as you're assuming that others working on your project will also have to have the same resources in their home directory ( if you're the only one working on your project and you insist on using |
That's not what I'm doing..here's some code to help explain. I have this in my tsconfig.json:
This works great everywhere except for when trying to run jest. So I ejected the config and added this to the "jest" section of package.json:
To be clear, I'm not referring to anyone's home folder on their computer; Anyway, regardless of how people feel about using ~ as an alias, my point is that it would be nice to be able to use the "moduleNameMapper" option without ejecting. People might also want custom aliases to deeply nested folders, for example. |
Unrelated to this thread (other than the same issue of Jest not compiling code successfully)... In case anyone is curious, the issue I referred to above turned out not to be related to slashes in import paths but rather .mjs files in node_modules. (I was confused because I first encountered the issue due to this line in my app: |
I'm running into this problem because I wrote my own library that exports ES6 style modules that I refer to directly in my Since |
@osdiab you could try using .mjs files, which support ES6 modules out of the box. The previous issue with using .mjs files in create-react-app (and create-react-app-typescript -- whether in your app code or in libraries) has been resolved. But this is just an idea; I haven't tried it and don't know if it will actually work with jest. It seems there are still some issues with jest and .mjs files: An easier short-term solution might be to tell jest to ignore your library, e.g. using this in package.json (assuming your library is in
(The latter solution is how I was able to get jest working with |
hmmm i didn't want to eject so i added the An import of a separate library, Why would that happen? I don't really know what the jest typescript transform under the hood is doing but everywhere else it seems fine to run |
Do you have |
I had it false, but turning it on caused a bunch of other errors in my project so I’d rather not. Eh maybe I’ll just eject :P |
In tsconfig.json |
Having this issue on my vuejs app. Making this configuration change under "jest" in my package.json fixed it.
|
@lopno I got it working using the following: in your "baseUrl": ".",
"paths": {
"src/*": ["src/*"]
} And in your "scripts": {
"start": "NODE_PATH=./ react-scripts-ts start",
"build": "NODE_PATH=./ react-scripts-ts build",
"test": "NODE_PATH=./ react-scripts-ts test --env=jsdom"
}, Notice the import Foo from 'src/components/Foo'` Hope this helps. |
@bjarkehs Hej Bjarke, long time no see. I got it working doing this: In
And adding a jest config in
|
This is working for me. |
Description
The apparent recommended solution for achieving absolute imports, most recently referenced in #119, is to add a
node_modules
folder or symlink insrc
. Because NODE_PATH doesn't work in Typescript, I believe this is the only way to do absolute imports in create-react-app-typescript.However, I am seeing jest tests choke when using such imports with
SyntaxError: Unexpected token export
errors.Weirdly this may be somehow related to using enums? See the reproducible demo below.
Expected behavior
Expect jest tests to work.
Actual behavior
Jest fails with:
Environment
[email protected]
node 8.2.1
npm 5.3.0
Reproducible Demo
I created a reproducible demo here:
https://github.com/eonwhite/absolute-imports
To reproduce the issue, git clone and npm install, then:
npm start
and note that the "app" works.npm test
and note that tests fail withSyntaxError: Unexpected token export
Note that you can get jest to run and the tests to pass by:
import { MyModel, Type } from './common/model/MyModel';
Type
enum insrc/common/model/MyModel.ts
(and the references to the enum in App.tsx)Obviously though I'm seeking a solution that lets me use absolute imports and enums in my code.
The text was updated successfully, but these errors were encountered: