2017-10-20 333 views
0

我嘗試使用WebStorm IDE在typescript(ES6)中編寫測試。例如爲:WebStorm,ES5/ES3中的異步函數或方法需要'Promise'構造函數

// Imports... 

describe('Message',() => { 
    const server = express(); 
    server.use(bodyParser.json()); 

    const messageService = { findAll:() => ['test'] }; 

    beforeAll(async() => { 
     const module = await Test.createTestingModule({ 
      modules: [MessageModule], 
     })... 
    }); 

    // Tests... 
}); 

然而WebStorm IDE顯示以下錯誤在async() =>

TS2705:在ES5/ES3異步函數或方法需要無極 構造。確保你有一個Promise 構造函數的聲明或者在你的--lib選項中包含ES2015。

tsconfig.json

{ 
    "compilerOptions": { 
    "module": "commonjs", 
    "declaration": false, 
    "noImplicitAny": false, 
    "removeComments": true, 
    "noLib": false, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "target": "es6", 
    "sourceMap": true, 
    "allowJs": true, 
    "outDir": "./dist" 
    }, 
    "include": [ 
    "src/**/*" 
    ], 
    "exclude": [ 
    "node_modules", 
    "**/*.spec.ts" 
    ] 
} 

我讀ts An async function or method in ES5/ES3 requires the 'Promise' constructor並嘗試添加

"lib": [ "es2015" ] 

但是它不具有任何影響。任何想法有什麼不對?

回答

4

添加

"lib": [ "es2015" ] 

tsconfig.json應該解決這個問題。 但是,您的規格文件似乎並未包含在您的tsconfig.json(檢查"include":[]"exclude":[]值)中。因此,Typescript服務必須爲您的文件使用不同的tsconfig.json(可能是默認文件,如果不存在tsconfig.json包含您的規格的文件可以找到) 要解決該問題,請確保指定配置中的lib屬性用於規範文件處理

+0

include/exclude是問題,謝謝lena :) –