Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting TypeError: foo_1.default is not a function #1873

Closed
TrueWill opened this issue Aug 11, 2020 · 10 comments
Closed

Getting TypeError: foo_1.default is not a function #1873

TrueWill opened this issue Aug 11, 2020 · 10 comments
Labels
Not An Issue Not ts-jest issue

Comments

@TrueWill
Copy link

TrueWill commented Aug 11, 2020

🐛 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"
@TrueWill TrueWill added Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Aug 11, 2020
@ahnpnl
Copy link
Collaborator

ahnpnl commented Aug 11, 2020

can you please try the latest version of ts-jest ?

@TrueWill
Copy link
Author

@ahnpnl I have; I've updated ts-jest, jest, and typescript with no change.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Aug 11, 2020

hmm this will be hard to check without any repos. Would you please try making a small repo for this issue ?

@ahnpnl
Copy link
Collaborator

ahnpnl commented 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.

@TrueWill
Copy link
Author

@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.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Aug 11, 2020

nice, I'm glad to hear that. So this issue can be also closed ?

@TrueWill
Copy link
Author

@ahnpnl Yes, it can be closed (unless circular references are a thing you want to support). Thanks again.

@ahnpnl ahnpnl added Not An Issue Not ts-jest issue and removed Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Aug 12, 2020
@ahnpnl ahnpnl closed this as completed Aug 12, 2020
@rogerprz
Copy link

rogerprz commented Oct 15, 2021

@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.

Can you explain how you found this out? What did your code look like. I am not sure of the changes I need to make on mine.
For me i'm getting TypeError: dayjs_1.default is not a function
iamkun/dayjs#1667
Thanks

@Sakari369
Copy link

Solved this error by searching and fixing circular dependencies using Madge:
https://andrejsabrickis.medium.com/locate-circular-dependencies-in-typescript-modules-2b1eb03dbf2e

@pulpdood
Copy link

pulpdood commented Jan 9, 2023

Found this on Google. I had this problem, but mine was probably an edge case: I had two files foo.ts and foo.json in the same directory and it was trying to grab the module from the JSON file. Just renamed the foo.json file and that fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not An Issue Not ts-jest issue
Projects
None yet
Development

No branches or pull requests

5 participants