Node.js 如何运行用TypeScript编写的Mocha测试?

Node.js 如何运行用TypeScript编写的Mocha测试?,node.js,typescript,mocha.js,Node.js,Typescript,Mocha.js,设置:我有一个用TypeScript编写的节点项目(纯节点,没有浏览器位)。我可以使用TypeScript模块中的TypeScript编译器(tsc)来编译代码。到目前为止还不错 然而,我想用Mocha编写测试,这就是我遇到的问题。我尝试了--编译器ts:typescript,但不断出现如下错误: error TS5023: Unknown compiler option 'compilers'. 看起来摩卡咖啡的命令行最后被传递到了tsc,这显然不好。不要使用这个答案。typescript

设置:我有一个用TypeScript编写的节点项目(纯节点,没有浏览器位)。我可以使用
TypeScript
模块中的TypeScript编译器(
tsc
)来编译代码。到目前为止还不错

然而,我想用Mocha编写测试,这就是我遇到的问题。我尝试了
--编译器ts:typescript
,但不断出现如下错误:

error TS5023: Unknown compiler option 'compilers'.

看起来摩卡咖啡的命令行最后被传递到了tsc,这显然不好。

不要使用这个答案。typescript require未维护,ts节点是其替换项。把这个答案留给子孙后代。

找到了。
typescript
模块实际上就像一个“main”函数;它在模块加载后立即运行编译器。不是很好的设计

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

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

对于任何尝试过typescript并遇到问题的人,您可能需要尝试一下


似乎也有人反对使用typescript require来支持ts node。

使用最新版本的Mocha和ts node时,我遇到了意外的令牌导入问题。使用以下设置对我有效:

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

我真的认为这不管用。你能明白为什么,别看这个。该项目已被否决。谢谢@DmitriiSorin。我已经编辑了我的答案,并向typescript提交了一份PR,要求自述文件更清楚地宣布这一点。另外,我刚刚注意到我最初问了这个问题,因此我可以移动绿色复选标记:D
——编译器也不推荐使用…>——要求ts节点/寄存器。。和/或使用
——需要源代码映射支持/注册
,以避免许多复杂问题。我已求助于编译用TypeScript编写的测试,并将tsc作为运行测试的前提,确保生成源代码映射。这允许我在VS代码中调试测试,而不依赖于typescript require或ts节点。typescript require已经被弃用了与“src/***.spec.ts”相关的test.ts是什么?@jpierson你能让你的过程成为完整答案吗?@JamelToms似乎不需要test.ts,最后一部分是找到项目中所有测试的模式。这对我有效:
mocha——要求ts节点/寄存器src/***。spec.ts
使用ts mocha对我也有效。我将我的解决方案添加到
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'));