【问题标题】:Issue with mocha test debugging from typescript来自打字稿的摩卡测试调试问题
【发布时间】:2019-02-20 18:06:25
【问题描述】:

我尝试调试 mocha 测试,但我遇到了问题,我不知道该如何解决。我之前在 google 上搜索过 stackoverflow,但没有任何成功。

错误是:

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.

tsconfig.json 文件如下所示:

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

vscode launch.json 配置是 vscode launch.json 配置是

{
      "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"
  }

测试文件:

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')
  })
})

我尝试了很多东西。就像启动器不加载 tsconfig。我试图在启动器配置中将“--lib”、“'ES2015'”作为 arg 传递。 谢谢。

【问题讨论】:

  • 我忘记测试了:
  • 我不确定,也许尝试将“ES2015.Promise”添加到tsconfig中的lib数组?
  • 没有。不行 。我在谷歌搜索后尝试过这个。看起来 tsconfig 没有加载。我试图弄清楚如何指定 mocha 来加载 tsconfg,但还没有成功。
  • 也许您应该指定您只想编译 TypeScript 测试文件?在我的 NPM 包脚本中,我声明了用于测试的特定文件:“test”:“mocha --timeout 10000 --colors --compilers ts:ts-node/register --exit ./tests/**/*. ts --full-trace --recursive",我也认为你可以从描述函数中省略异步
  • 我的 tsconfig: { "compilerOptions": { "sourceMap": true, "baseUrl": "src", "experimentalDecorators": true, "target": "es6", "module": " commonjs", "declaration": true, "emitDecoratorMetadata": true, "allowJs": false, "outDir": "lib" } }

标签: typescript debugging visual-studio-code mocha.js


【解决方案1】:
更新:这个答案是旧的。现在可能有更好的方法来解决这个问题。我最近没有测试过。

错误信息清楚地告诉你该怎么做:

确保您有“Promise”构造函数的声明或在 --lib 选项中包含“ES2015”

既然我们绝对不想提供自己的定义,我们应该继续在选项中包含“ES2015”。

怎么做?

所有这些 ts-node suff 都非常复杂。有经验的人可能会告诉如何指向特定的tsconfig,但我不能。我建议设置一个环境变量来实现这个目标。

我深入源码发现需要的env var是TS_NODE_COMPILER_OPTIONS,意思是在调用ts-node时,process.env必须有这个属性:

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

那该怎么做呢?

使用 VS Code 进行调试时,您可以通过在配置中添加以下内容来实现此目标:

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

当您想要运行测试时,例如使用npm test,您可以在使用cross-env 运行测试脚本之前将此选项添加到环境变量中。

我最终在package.json 中写了这样的内容:

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

注意 - 三重反斜杠是有意的

【讨论】:

  • 您好,感谢您的回答。我会尝试您的解决方案,因为我没有解决问题。但我只是暂时忽略了。
  • @dudufux 请考虑回复这是否解决了您的问题,如果可以,请接受答案。
  • 按照指定添加 TS_NODE_COMPILER_OPTIONS 环境变量为我解决了这个问题。我通过 bash 配置文件设置它,但应该以任何方式工作
  • 这有点问题,因为我注意到它没有使用我的 tsconfig 构建位置,而是将编译的 .js 文件转储到我的 src 目录中。由于这个原因,这可能不是一个可接受的解决方案
  • @Steve 你可以直接在你的launch.json中设置环境变量:“env”:{“TS_NODE_PROJECT”:“path/to/tsconfig.json”}。这将导致它尊重您的 tsconfig 并将文件放置在正确的位置。如果您需要特定于测试的配置,这还允许您指定专门用于测试的 tsconfig.test.json 文件。
猜你喜欢
  • 2019-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多