unflores
unflores

Reputation: 1810

Jest does not recognize my custom typings

I have a typescript project which is requiring a personal library that is added to my package.json from its git repo built as a commonjs lib. When I run my build script ./node_modules/.bin/webpack -d --config webpack/webpack.dev.js, everything compiles correctly. However, when I run jest jest --config front/jest.config.js, it throws the following error:

● Test suite failed to run
TypeError: dumblib_1.default is not a constructor

12 | const dumblib = new DumbLib(vars)

When go into the entrypoint of the lib which is just a node project compiled from es6 to commonjs, the bundle shows this at the end of the file:

module.exports = DumbLib;

The lib isn't typescript so I have a typings file in my main project for the dumblib import.

declare module 'dumblib' {
  .
  .
  .

  export default class DumbLib {
    constructor(vars: Vars)
    .
    .
    .
  }

}

Like I said, it seems that the typescript compiler sees it as everything compiles correctly. However, jest keeps throwing that error. Is there something special that I have to do for jest to recognize my typings? Or is there maybe some other inconsistency that I am not seeing?

Upvotes: 0

Views: 1091

Answers (1)

Dmitriy
Dmitriy

Reputation: 2822

The problem here is that actual export of your module does not match with typings. TypeScript expect a "default" exports from a module because of the typings (you say export default), but then inside the module class is the whole export.

Doing these 2 things should help:

  1. When you import the module make sure it is like this:
import * as dumblib from "dumblib";
  1. in your type definition it should be:
declare module 'dumblib' {

  class DumbLib {
    // ...
  }

  namespace DumbLib {
    interface Foo {
      bar: string
    }
  }

  export = DumbLib
}

Upvotes: 1

Related Questions