Jest do not see TypeScript classes

How to setup Jest to see my TypeScript classes placed in different files without import/export of those classes? I have bunch of TS classes in separate files. I can not use "import" or "export" of those classes (because of the JS interpreter which we use by company policy).

So I can not write:

export class MyClass {

}

and then use it in my tests like it is proposed:

import {MyClass} from "../MyClass"

test("MyClass test", () =} {
    let mc = new MyClass()
    // ...
})

Is there a way to explain Jest where to look for classes? Because TypeScript itself works well and compiles all of those classes and sees them.

2 answers

  • answered 2018-12-05 21:39 Connor Ross

    What do you mean by company policy? How can you not use import / export in your tests? Are your tests not written in typescript as well? Are you running your code through the typescript compiler?

    Jest only understands javascript, so you have to get your typescript to javascript using the typescript compiler (tsc). There is a node module ts-jest that does this.

  • answered 2018-12-06 15:22 Ryan Martin

    This guide helped me immensely when I needed to get Typescript set up with Jest. It includes all the dependencies and config you need to get up and running. Hope it helps!