2017-08-09 74 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