Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jest + typescript + es6 modules (yet again, 2019) - SyntaxError: Unexpected token export

I'm still facing problems trying to use jest, typescript and es6 modules. My test are written in Typescript, I'm trying to import objects from js-file which uses es6 modules. To do that, I've followed jest documentation and have the following in my babel.config.js, which is located in project root:

module.exports = {
presets: [
    [
        '@babel/preset-env',
        {
            targets: {
                node: 'current'
            }
        }
    ]
]

};

I have the following dependencies installed:

"devDependencies": {
  "@babel/core": "^7.2.2",
  "@babel/preset-env": "^7.3.1",
  "@types/jest": "^23.3.13",
  "@types/node": "^10.12.18",
  "babel-jest": "^24.1.0",
  "jest": "^23.6.0",
  "rollup": "^1.1.2",
  "rollup-plugin-terser": "^4.0.4",
  "rollup-plugin-typescript2": "^0.19.2",
  "ts-jest": "^23.10.5",
  "ts-node": "^8.0.1",
  "typescript": "^3.2.4"
}

In jest.config.js I have the following:

module.exports = {
verbose: true,
transform: {
    "^.+\\.jsx?$": "babel-jest",
    '^.+\\.ts?$': 'ts-jest'
},
testEnvironment: 'node',
testRegex: '/test/.*\\.(test|spec)?\\.(ts|tsx)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
};

And finally, I'm trying to import like this in my tests:

import {Dag, Directions} from "../dist/dag";

And here it says: SyntaxError: Unexpected token export

D:\devel\tomtom_dag\dist\dag.js:72
}();export { e as Directions, t as Action, n as Dag };

Can someone say what am I doing wrong or point to the project with similar config? And yes, I've googled this already for several hours and have tried solutions from here, here and have tried with this plugin - with the same result=(

UPDATE: I've created a project to reproduce the issue: https://github.com/AntonPilyak/jest_typescript_es6 Also, I've noticed that I had forgot to mention that I was trying to use jest@23 version due to ts-jest can't work with the latest (gives a warning + I get an empty test suite). Maybe, my problems are coming out from that fact. But still, I'm unable to make a proper config: if I use the latest jest I get a warning + jest says that the test suite is empty and test is passing though it shouldn't.

like image 393
Anton Pilyak Avatar asked Feb 15 '19 14:02

Anton Pilyak


1 Answers

It works with ts-jest 24 + it appears to be a bug in my IDE: https://youtrack.jetbrains.com/issue/IDEA-207553

like image 176
Anton Pilyak Avatar answered Nov 14 '22 05:11

Anton Pilyak