2017-08-09 71 views
0

我在我的Typescript应用程序中使用了已编译的ES6库。测试失败,错误ts-jest无法在导入的ES6模块上运行测试

TypeError: Cannot read property 'PropTypes' of undefined

则抱怨该模块导入react

import React from 'react'; 

如果我改变了对

import * as React from 'react'; 

然后它会编译并运行良好。这是我package.jsonjest配置:

"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

then it will compile and run fine.

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

+0

它迟早会破。如果可能的话应该避免。 Webpack和SystemJS启用了“反应”导入反应,因此任何使用这两种工具的人都应该更喜欢用于导入_non-namespace_导入https://github.com/nodejs/node-eps/的语法。 –

+0

@AluanHaddad不是'import反应从'反应''当使用'原始TypeScript transpile - >现在运行在节点'''不行*'? – basarat

+0

@basarat,这是我通过'npm'安装的模块。 Typescript假设可以导入JS模块。事实上是这样。该应用程序编译好。这是笑话测试失败。 – Kousha