2017-10-20 326 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 :) –