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.

Getting TypeError: foo_1.default is not a function

See original GitHub issue

🐛 Bug Report

While refactoring a node project, I moved some files around (still in subfolders of src, adjusting the imports).

After doing so, I get an error when running tests of

Test suite failed to run TypeError: foo_1.default is not a function

(foo is my function, renamed for simplicity)

To Reproduce

jest.config.js:

module.exports = {
  "roots": [
    "<rootDir>/src"
  ],
  "transform": {
    "^.+\\.tsx?$": "ts-jest"
  },
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",                          
    "module": "commonjs",                     
    "lib": [ "es2017", "es2015.promise", "es2015.collection", "es5" ],
    "declaration": true,                      
    "outDir": "./build",
    "rootDir": "./src",                       
    "strict": true,                           
    "noImplicitAny": true,                    
    "strictNullChecks": true,                 
    "noImplicitThis": true,                   
    "alwaysStrict": true,                     
    "noUnusedLocals": false,                   
    "noUnusedParameters": false,               
    "noImplicitReturns": true,                
    "noFallthroughCasesInSwitch": true,
    "composite": true,
    "declarationMap": true,
    "sourceMap": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true
  }
}

The error occurs in module1.ts:

  import foo from './foo';
  const x = foo(bar);

Foo is defined in foo.ts:

export default function foo() {}

The test in question is in foo.spec.ts (all in the same directory):

import { bar } from './foo';

// Test bar, which does _not_ call foo()
  • Other tests work.
  • These tests worked until I moved the files (renamed the directory under src they were in).
  • tsc works.
  • Things I’ve tried:
    • Changing the import to a non-default (results in “TypeError: foo_1.foo is not a function”)
    • "allowSyntheticDefaultImports": true
    • "esModuleInterop": true
    • Updating ts-jest, Jest, and TypeScript to the latest versions
    • Using a moduleNameMapper (tsconfig-paths-jest and other permutations)

Expected behavior

The test works.

Link to repo (highly encouraged)

(Private/proprietary; can’t)

Debug log:

(Can’t include - references proprietary info)

envinfo

System:
    OS: MacOS Catalina

Npm packages:
   **(Note: I've tried upgrading all of these but node to the latest releases with no change.)**
    jest: 24.9.0
    ts-jest: 24.1.0
    typescript: 3.6.4
    babel(optional): n/a
    node: 12.16.3

Also reproduced with:
    "jest": "^26.3.0",
    "ts-jest": "^26.2.0",
    "typescript": "^3.9.7"

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

13reactions
TrueWillcommented, Aug 11, 2020

@ahnpnl I found the issue. In attempting to simplify to a small repo that would reproduce this, I found a circular reference.

  • module1.ts imports from module2.ts
  • module2.ts imports from module3.ts
  • module3.ts imports from module1.ts

This works with tsc but fails with Jest + ts-jest.

Eliminating the circular reference (by moving one export to another module) resolved the issue. Thank you.

4reactions
ahnpnlcommented, Aug 11, 2020

normally the combination of allowSyntheticDefaultImports": true and esModuleInterop: true should solve the problem. But I don’t know exactly why it doesn’t work for that file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: (0 , dayjs_1.default) is not a function - Stack Overflow
It was working on my previous app, but I don't know why do I get this error for my new application? node.js ·...
Read more >
Functions - JavaScript - MDN Web Docs
A function without a return statement will return a default value. In the case of a constructor called with the new keyword, the...
Read more >
TypeError - (0 , _requireHook).default is not a function
I'm getting TypeError: (0 , _requireHook).default is not a function at Object.
Read more >
Writing tests: type error not a function - JavaScript
The error says that sortEvents is not a function even though it is: ... get to miss off the curly brackets if you...
Read more >
typeerror: (0 , express_1.default) is not a function - You.com
While testing some endpoints I have on a TypeScript Express server, I keep getting the error when trying to pass the app 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