Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript Jest failing on TypeError: Class extends value undefined is not a constructor or null

I am using ts-jest to write tests for my ts-node project. The application runs fine and there are no TypeScript errors in the IDE (VSCode). However, whenever I try to run my test suite I get the following error

Test suite failed to run

    TypeError: Class extends value undefined is not a constructor or null

It then points to a random class in my code. I understand this to be a circular dependency issue, and a quick use of Madge confirms that my project has several circular dependencies. I know that this is poor code quality, but I just need these tests to run. My frustration is that the code can clearly run despite these circular dependencies, is there any way to fix this or disable Jest from failing on this TypeError?

like image 525
StanFlint Avatar asked Oct 15 '25 20:10

StanFlint


1 Answers

Typically this is due to a circular dependancy issues. The confusing bit is that your code may still build and run fine, but testing it does reveal the issue.

Make sure you Sub-class and Base-class do not import or refer to each other's files and exports.

Common mistake is to export your definitions using index.ts like:

export * from './base-class'
export * from './sub-class'
export * from './utilities'

Then in you Base-class you import using index:

import { Utility } from './index'

This will create a circular dependancy and may cause the above issue. Instead, try importing from files directly.

like image 148
Samvel Avanesov Avatar answered Oct 18 '25 08:10

Samvel Avanesov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!