Kousha
Kousha

Reputation: 36299

ts-jest cannot run testing on imported ES6 modules

I'm using a compiled ES6 library in my Typescript application. The testing fails with an error

TypeError: Cannot read property 'PropTypes' of undefined

The module it is complaining about is importing react as

import React from 'react';

If I change this to

import * as React from 'react';

then it will compile and run fine. Here is my package.json's jest config:

"jest": {
    "transform": {
      ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js|jsx)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json"
    ],
    "verbose": true,
}

And here is my tsconfig.json:

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "module": "commonjs",
        "target": "es5",
        "jsx": "react",
        "allowJs": true,
        "preserveConstEnums": true,
        "removeComments": true,
        "noImplicitAny": false,
        "moduleResolution": "node",
        "noUnusedParameters": true
    },
    "include": [
        "./src/**/*"
    ],
    "filesGlob": [
        "**/*.ts",
        "**/*.tsx"
    ],
    "exclude": [
        "node_modules",
        "typings/main",
        "typings/main.d.ts",
        "typings/index.d.ts"
    ]
}

What configuration have I messed up here?

Upvotes: 1

Views: 2010

Answers (1)

basarat
basarat

Reputation: 276363

then it will compile and run fine.

That is the way to go for TypeScript. import * as React from "react".

Upvotes: 1

Related Questions