ts-jest无法对导入的ES6模块进行测试

时间:2017-08-09 02:11:08

标签: javascript reactjs typescript jest

我在我的Typescript应用程序中使用已编译的ES6库。测试失败并出现错误

  

TypeError:无法读取未定义

的属性“PropTypes”

它抱怨的模块是将react导入为

import React from 'react';

如果我将其更改为

import * as React from 'react';

然后它将编译并运行正常。这是我的package.json jest配置:

"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,
}

这是我的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"
    ]
}

我搞砸了什么配置?

1 个答案:

答案 0 :(得分:1)

  

then it will compile and run fine.

这是TypeScript的方法。 import * as React from "react"