12

I try to write tests in typescript (ES6) using WebStorm IDE. E.g.:

// Imports...

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

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

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

    // Tests...
});

However WebStorm IDE shows following error at async () =>

TS2705: An async function or method in ES5/ES3 requires the Promise constructor. Make sure you have a declaration for the Promise constructor or include ES2015 in your --lib option.

My 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"
  ]
}

I read ts An async function or method in ES5/ES3 requires the 'Promise' constructor and tried adding

"lib": [ "es2015" ]

however it does not have any effect. Any ideas what's wrong?

LazyOne
  • 158,824
  • 45
  • 388
  • 391
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601

4 Answers4

21

Solution without editing the project sources

I had this problem with IntelliJ and resolved by changing my IDE settings:

Settings -> Language & Frameworks -> TypeScript

then in the "Options" field add:

--lib es2015

enter image description here

Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
  • this answer! My problem wasn't the code, but WebStorm! my code ran fine through WebStorm even though it was giving this error – Limey Apr 06 '20 at 20:02
19

Adding

"lib": [ "es2015" ]

to tsconfig.json should fix the issue. However it seems that your spec files are not included in your tsconfig.json (check "include":[] and "exclude":[] values). So the Typescript service must be using a different tsconfig.json for your files (may be a default one, if no tsconfig.json files that include your specs can be found) To fix the issue, make sure to specify the lib property in config that is used for your spec files processing

lena
  • 90,154
  • 11
  • 145
  • 150
0
{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./src",
    "lib": ["es2015"]
  },
  "include": [
    "src/**/*",
    "**/*.spec.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

adding "lib":["es2015"] to "compilerOptions" as per @lena's answer and Removing **/*.spec.ts from "exclude":[] and adding it to "include":[] solved it for me.

Manu Nair
  • 70
  • 2
  • 4
-1

Add "lib": [ "es2015" ] in tsconfig.json under compilerOptions as

{
    "compilerOptions": {
      "lib": [ "es2015" ]
  }
}
Ihsan Rehman
  • 77
  • 10