jest tests failing with "Unexpected token export" when using absolute imports #122
Description
Description
The apparent recommended solution for achieving absolute imports, most recently referenced in #119, is to add a node_modules
folder or symlink in src
. 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:
SyntaxError: Unexpected token export
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
at Object.<anonymous> (src/App.tsx:15:17)
at Object.<anonymous> (src/App.test.tsx:5:13)
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:
- Try
npm start
and note that the "app" works. - Try
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:
- changing the absolute import on line 5 of App.tsx to a relative import:
import { MyModel, Type } from './common/model/MyModel';
- removing the
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.