Issue with mocha test debugging from typescript

Viewed 1193

I trying to debug a mocha test but I have an issue and i don't know how can i solve it. I search on google before and on stackoverflow but without any success.

The error is :

TSError: ⨯ Unable to compile TypeScript:
source-map-support.js:444 error TS2468: Cannot find global value 'Promise'.backend/test/textToSpeech/lib.ts(11,30): error 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.backend/test/textToSpeech/lib.ts(12,27): error 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.

The tsconfig.json file looks like:

{
"compilerOptions": {
  "module": "commonjs",
  "watch": true,
  "noImplicitAny": false,
  "removeComments": true,
  "outDir": "./dist",
  "sourceMap": true,
  "target": "es6",
  "lib": [
      "ES2015"
  ],
  "types": [
    "node",
    "pg-promise"
  ],
  "typeRoots": [
    "node_modules/@types"
  ]
  },
  "include": [
  "src/**/*"
 ],
"exclude": [
  "node_modules",
  "**/*.spec.ts"
 ]
}

And the vscode launch.json config is And the vscode launch.json config is

{
      "type": "node",
      "request": "launch",
      "name": "Mocha Tests",
      "program": "${workspaceFolder}/backend/node_modules/mocha/bin/_mocha",
      "args": [
          "--require", "ts-node/register",
          "-u",
          "tdd",
          "--timeout",
          "999999",
          "--colors",
          "${workspaceFolder}/backend/test/textToSpeech/lib.ts"
      ],
      "internalConsoleOptions": "openOnSessionStart"
  }

The test file:

import {} from 'mocha'
import { expect } from 'chai'
import config from '../configuration'
import { TextToSpeechLib } from '../../src/libs/textToSpeech/'
var textToSpeach = new TextToSpeechLib(config)

var text = 'Hello world'


describe('TextToSpeach lib', async () => {
  it ('Convert text ...', async () => {
    console.log("==== =a= =s= a==")
    let resp = await textToSpeach.convertText(text);
    expect(resp.status).to.be.equal('success')
  })
})

I tried a lot of things. Is like the launcher don't load tsconfig. I tried to pass "--lib","'ES2015'" as arg in launcher config. Thank you.

1 Answers
Update: This answer is old. There might be better ways to solve this problem by now. I have not tested it recently.

The error message clearly tells you what to do:

Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your --lib option

Since we definitely do not want to supply our own definitons, we should go ahead and include 'ES2015' in the options.

How to do that?

All this ts-node suff is very involved. Somebody experienced in the matter could probably tell how to point to a specific tsconfig, I however can not. I suggest setting an environment variable to achieve this goal.

I digged into the source code to find out the neccessary env var is TS_NODE_COMPILER_OPTIONS, meaning while invoking ts-node, process.env must have the attribute:

TS_NODE_COMPILER_OPTIONS={"lib": ["ES2015"], ...}

And how to do that?

When debugging using VS Code, you may achieve this goal by adding the following to your configuration:

"env": {"TS_NODE_COMPILER_OPTIONS":"{\"lib\": [\"ES2015\"]}"}

When you want to run your tests, e.g. using npm test, you can add this option to the environment vars prior to running the test script using cross-env.

I ended up writing something like this in my package.json:

"scripts": {
    "test": "cross-env TS_NODE_COMPILER_OPTIONS=\"{\\\"lib\\\": [\\\"ES2015\\\"]}\" mocha --require ts-node/register path/to/test(s).ts"
}

Note - Triple backslashses are intentional

Related