Ts-jest: Cannot import packages from node_modules

Created on 15 May 2018  路  5Comments  路  Source: kulshekhar/ts-jest

  • Issue

When I run a test with code that calls a function from a module in node_modules, I get a type error telling me it's not a function. This doesn't happen functions or variables imported from within the codebase. Take this for example:

import jwt_decode from 'jwt-decode';
jwt_decode('token');

This error is tossed:

TypeError: jwt_decode_1.jwt_decode is not a function

This only happens inside the context of Jest and works fine with compiled code. In this case, my bundler of choice is Parcel.

It should be noted that requireing the modules does work.

  • Expected behavior

I expect the functions to import normally and for my tests to pass.

  • Output from your debug log
[
  "Reading tsconfig file from path /Users/eligundry/Code/fun-messenger/tsconfig.json"
]
[
  "Original typescript config before modifications: ",
  {
    "allowJs": true,
    "jsx": 2,
    "target": 1,
    "module": 1,
    "strict": true,
    "lib": [
      "lib.dom.d.ts",
      "lib.es5.d.ts",
      "lib.es2015.d.ts",
      "lib.es2016.d.ts"
    ]
  }
]
[
  "final compilerOptions:",
  {
    "allowJs": true,
    "jsx": 2,
    "target": 1,
    "module": 1,
    "strict": true,
    "lib": [
      "lib.dom.d.ts",
      "lib.es5.d.ts",
      "lib.es2015.d.ts",
      "lib.es2016.d.ts"
    ]
  }
]
[
  "final compilerOptions:",
  {
    "allowJs": true,
    "jsx": 2,
    "target": 1,
    "module": 1,
    "strict": true,
    "lib": [
      "lib.dom.d.ts",
      "lib.es5.d.ts",
      "lib.es2015.d.ts",
      "lib.es2016.d.ts"
    ],
    "inlineSourceMap": true,
    "inlineSources": true
  }
]
[
  "tsJestConfig: ",
  {
    "useBabelrc": true
  }
]
[
  "Compiling via normal transpileModule call"
]
[
  "Using babel with options:",
  {
    "babelrc": true,
    "plugins": [],
    "presets": []
  }
]
  • Link to a minimal repo that reproduces this issue

https://github.com/eligundry/fun-messenger

$ npm install
$ npm test
  • Optional (but highly recommended) - Configure Travis (or your favorite system) with the minimal repo

This allows potential solutions to be tested against the minimal repo. This saves everyone time and avoids a lot of back and forth.

Most helpful comment

Using the last version and enabling esModuleInterop in tsconfig should fix your issue. If this is not the case, please re-open this issue ;-)

All 5 comments

@eligundry I'd like to look into this but without a minimal repo, I'm hesitant because there have been several instances where looking at a larger project has resulted in false leads and wasted time.

If you can replicate this issue with just a couple of files in src and test folders, I'll be happy to take a look at it after/before work. Without that, I'll have to wait till I can set aside a longer window of time.

@kulshekhar fair enough. Here is an extremely simplified test case of the above issue.

https://github.com/eligundry/ts-jest-test-case

# To reproduce, run this.
$ npm run test

It should also be noted that this is happening with all modules, not just jwt-decode, but it was the easiest one to setup.

Not sure whether this is relevant to the issue, but I鈥檓 having the same problem using webpack for module bundling.

Using the last version and enabling esModuleInterop in tsconfig should fix your issue. If this is not the case, please re-open this issue ;-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bySabi picture bySabi  路  4Comments

AlexGellert picture AlexGellert  路  4Comments

jbreckmckye picture jbreckmckye  路  3Comments

mikeyakymenko picture mikeyakymenko  路  3Comments

stephenotalora picture stephenotalora  路  3Comments