question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not processing correctly when using es2015 modules

See original GitHub issue

Relates to #99

When using es2015 modules (necessary for example for tree-shaking), ts-jest throws an error not recognizing import statement. Such configuration can be:

{
    "compilerOptions": {
        "moduleResolution": "node",
        "target": "es6",
...

But when you use commonjs modules, it works

{
    "compilerOptions": {
        "module": "commonjs"
        "moduleResolution": "node",
        "target": "es6",
...

How to reproduce

git clone -b ts-jest-issue https://github.com/alexjoverm/typescript-library-starter.git
cd typescript-library-starter

npm install # give it the name "library" when asked
npm t

It will throw a SyntaxError: Unexpected token import

Now go to tsconfig.json and add "module": "commonjs" to the compilerOptions property. Execute npm t -- --no-cache

Then it works

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
alexjovermcommented, Mar 6, 2017

Ok, understood. I think I’ve done it in jest but you need babel-jest and all that stuff if I remember correctly.

If that’s the case, probably is better to add it to https://github.com/kulshekhar/ts-jest#known-limitations-for-ts-compiler-options?? Then other people don’t stumble into the same issue

0reactions
piotrwitekcommented, Apr 9, 2017

@kulshekhar @alexjoverm isn’t this commit introduced a regression to related PR #123? https://github.com/kulshekhar/ts-jest/commit/d26dbf6ff0b10a1a30f74274caef67e55d43bc29#diff-e1495d267619047a7cca5cfe8f692729L85

I have the same issue as OP, from what I understood the change was suppose to always override module entry, no matter if tsconfig is loaded from root folder or other folder like src/ in my case. I think the change from the commit changed to work only for tsconfig loaded from root folder. This change has broken my test config after the update.

My config:

// jest
{
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js"
  ],
  "transform": {
    ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
  },
  "globals": {
    "__TS_CONFIG__": "<rootDir>/src/tsconfig.json"
  }
}

//tsconfig
{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "moduleResolution": "node",
    ...
  }
}

I have created a new issue to track this: https://github.com/kulshekhar/ts-jest/issues/161

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - ES2015 not compiled correctly - Stack Overflow
I have this line in a .jsx file in my react project: import React, {Component} from 'react';. However, this is not compiled correctly...
Read more >
JavaScript modules - MDN Web Docs
This guide gives you all you need to get started with JavaScript module syntax.
Read more >
16. Modules - Exploring JS
Programmatically work with modules; Configure module loading. The module loader API is not part of the ES6 standard #. It will be specified...
Read more >
Modules, introduction - The Modern JavaScript Tutorial
A module is just a file. One script is one module. As simple as that. Modules can load each other and use special...
Read more >
Documentation - Modules - TypeScript
declared in a module are not visible outside the module unless they are explicitly exported using one of the export forms. Conversely, to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found