【问题标题】:How to run Mocha tests written in TypeScript?如何运行用 TypeScript 编写的 Mocha 测试?
【发布时间】:2023-03-31 15:14:01
【问题描述】:

设置:我有一个用 TypeScript 编写的 Node 项目(纯 Node,没有浏览器位)。我可以使用 typescript 模块中的 TypeScript 编译器 (tsc) 来编译代码。到目前为止一切顺利。

但是,我想使用 Mocha 编写测试,这就是我遇到问题的地方。我试过--compilers ts:typescript,但我不断收到如下错误:

error TS5023: Unknown compiler option 'compilers'.

看起来mocha 的命令行最终被传递给tsc,这显然不好。

【问题讨论】:

    标签: node.js typescript mocha.js


    【解决方案1】:

    不要使用这个答案。 typescript-require 是无人维护的,而 ts-node 是它的替代品。将这个答案留给后代。

    找到了。 typescript 模块实际上就像一个“主”函数;一旦加载模块,它就会运行编译器。不是很好的设计。

    我戳了一下 Mocha 的验收测试,它展示了如何为 foo 文件使用自定义编译器。他们通过require.extensions 机制将其连接起来。我正在编写一个只在命令行上调用tsc 的模块时,我意识到以前一定有人这样做过。所以很简单:

    $ npm install typescript-require --save-dev
    $ mocha --compilers ts:typescript-require
    

    【讨论】:

    • 我真的不认为这很好用。您可以查看my related question 了解原因。
    • 别看这个。该项目已弃用。
    • 谢谢@DmitriiSorin。我已经编辑了我的答案,并向 typescript-require README 提交了一个 PR,以便更清楚地宣布这一点。另外我刚刚注意到我最初提出了这个问题,所以我可以移动绿色复选标记:D
    • --compilers 也已弃用...>--require ts-node/register 他们说.. 和/或--require source-map-support/register
    【解决方案2】:

    对于尝试过 typescript-require 并遇到问题的任何人,您可能想尝试ts-node

    $ npm install -g ts-node
    $ mocha --require ts-node/register src/**/*.spec.ts
    

    似乎还有一些 ongoing discussion 关于弃用 typescript-require 以支持 ts-node。

    【讨论】:

    • 为了避免很多复杂性,我采用了以 tsc 作为运行测试的前导来编译我的测试,这些测试是用 TypeScript 编写的,并确保生成源映射。这允许我在 VS Code 中调试我的测试,并且不依赖 typescript-require 或 ts-node。
    • 什么是 test.ts 与 'src/**/*.spec.ts' 的关系?
    • @jpierson 你能让你的过程成为一个完整的答案吗?
    • @JamelToms 似乎不需要test.ts,最后一部分是在项目中查找所有测试的模式。这对我有用:mocha --require ts-node/register src/**/*.spec.ts
    【解决方案3】:

    使用最新版本的 Mochats-node 我遇到了 Unexpected token 导入问题。使用ts-mocha 的以下设置对我有用:

    tsconfig.json

    {
        "files": [
            "src/main.ts"
        ],
        "compilerOptions": {
            "noImplicitAny": true,
            "target": "es2015",
            "types": ["mocha"],
            "module": "commonjs"
        }
    }
    

    package.json

    "scripts": {
        "mocha": "ts-mocha -p library/tsconfig.json library/test/**/*.ts"
      },
    

    launch.json

    {
        "type": "node",
        "request": "launch",
        "name": "Mocha Tests",
        "runtimeArgs": [
            "${workspaceFolder}/node_modules/ts-mocha/bin/ts-mocha",
            "--timeout", "999999",
            "-p",
            "${workspaceFolder}/library/tsconfig.json",
            "${workspaceFolder}/library/test/**/*.ts"
        ],
        "internalConsoleOptions": "openOnSessionStart"
    }
    

    还有 gulp.js 以防你也想使用 gulp

    const gulp = require('gulp');
    const ts = require('gulp-typescript');
    const mocha = require('gulp-mocha');
    
    const tsProject = ts.createProject('tsconfig.json');
    
    gulp.task('build', () => tsProject.src()
      .pipe(tsProject())
      .js.pipe(gulp.dest('dist')));
    
    gulp.task('test', () => gulp.src('test/*.spec.ts')
      .pipe(mocha({
        reporter: 'nyan',
        require: ['ts-node/register'],
      })));
    /* single command to hook into VS Code */
    gulp.task('default', gulp.series('build', 'test'));
    

    【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2018-07-24
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多