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.
I expect the functions to import normally and for my tests to pass.
[
"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": []
}
]
https://github.com/eligundry/fun-messenger
$ npm install
$ npm test
This allows potential solutions to be tested against the minimal repo. This saves everyone time and avoids a lot of back and forth.
@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 ;-)
Most helpful comment
Using the last version and enabling
esModuleInteropin tsconfig should fix your issue. If this is not the case, please re-open this issue ;-)