【问题标题】:Cannot test typescript with mocha syntax error无法使用 mocha 语法错误测试打字稿
【发布时间】:2018-09-20 15:23:08
【问题描述】:

我已经像这样设置了我的 mocha 测试命令

mocha --require test/ts-node-hooks.js test/**/*.spec.ts

我的ts-node-hooks.js 文件也是这样

const path = require('path');
require("ts-node").register({
    project: path.resolve(__dirname, 'tsconfig.json'),
});

我在/test 目录中的tsconfig.json 文件设置为使用ESNEXT 作为javascript 目标

{
  "compilerOptions": {
    /* Basic Options */
    "target": "ESNEXT",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "types": ["@3846masa/axios-cookiejar-support"]                           /* Type declaration files to be included in compilation. */
  }
}

但我不断收到此错误

$ mocha --require test/ts-node-hooks.js test/**/*.spec.ts
/src/Call.ts:41
            return (async () => this._callClass = await this.getCallValue('callclass'))();
                          ^
SyntaxError: Unexpected token (

tsc version 2.6.2编译代码没有问题。

【问题讨论】:

    标签: typescript mocha.js ts-node


    【解决方案1】:
    1. 使用mocha -compilers <path to ts-node> 例如。 node_modules\ts-node\register test/test-node-hooks.ts

    2. 如果上面的方法不行,用tsc test-node-hooks.ts重新编译.ts文件,再次运行mocha命令。

    【讨论】:

    • 感谢您的建议,如果问题再次出现并且我的回答没有解决它,我会记住这一点。
    【解决方案2】:

    就我而言,这是一个简单的问题,即我忘记了现有的全局安装ts-node。卸载解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 2019-04-18
      • 2018-12-20
      • 2017-09-29
      • 2021-09-21
      • 2019-10-26
      • 2020-11-09
      相关资源
      最近更新 更多