ts-jest: regression of Unexpected token import when using es2015 modules

  • Issue regression of previously resolved issue #121 #123

SyntaxError: Unexpected token import when using es2015 modules with following config:

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

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


  • Fix If following line is taken out of enclosing if statement, problem is fixed.

https://github.com/kulshekhar/ts-jest/commit/d26dbf6ff0b10a1a30f74274caef67e55d43bc29#diff-e1495d267619047a7cca5cfe8f692729R88

  • Expected behavior

should set config.module = 'commonjs'; when loading external tsconfig with "module": "es2015"

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 17 (11 by maintainers)

Most upvoted comments

@kulshekhar count on me if you need help for that refactor, you’re not alone 😉 I’d like to help if I can

@kulshekhar I understand, this is not an easy work, I really appreciate your work don’t get me wrong, just wanted to suggest a way to improve it somehow, maybe it’s not the best solution but might be some direction to follow 😃

Suggested workaround is the best solution possible right now and should be fine for now, and I’m still open to help in the future.

In the worst case - suppose you need all these settings for testing as well - you could create a separate file for testing, like so:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "commonjs"
  }
}

and use this new file in jest > globals > __TS_CONFIG__