如何运行用 TypeScript 编写的 Mocha 测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26977722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run Mocha tests written in TypeScript?
提问by Thomas
Setup: I have a Node project (pure Node, no browser bits) written in TypeScript. I can use the TypeScript compiler (tsc
) from the typescript
module to compile the code. So far so good.
设置:我有一个用 TypeScript 编写的 Node 项目(纯 Node,没有浏览器位)。我可以使用模块中的 TypeScript 编译器 ( tsc
)typescript
来编译代码。到现在为止还挺好。
However, I want to write tests using Mocha, and that's where I'm having trouble. I tried --compilers ts:typescript
, but I keep getting errors like:
但是,我想使用 Mocha 编写测试,这就是我遇到麻烦的地方。我试过了--compilers ts:typescript
,但我不断收到如下错误:
error TS5023: Unknown compiler option 'compilers'.
It looks like the command line to mocha
ends up being passed to tsc
, which is obviously not good.
看起来命令行 tomocha
最终被传递给tsc
,这显然不好。
回答by jpierson
For anybody who has tried and had problems with typescript-requireyou may want to try ts-node.
对于任何尝试过typescript-require并遇到问题的人,您可能想尝试ts-node。
$ npm install -g ts-node
$ mocha test.ts --require ts-node/register src/**/*.spec.ts
It also appears that there has been some ongoing discussionabout deprecating typescript-require in favor of ts-node.
似乎也有一些关于弃用 typescript-require 以支持 ts-node 的持续讨论。
回答by Thomas
Don't use this answer. typescript-require is unmaintained, and ts-node is its replacement. Leaving this answer here for posterity.
不要使用这个答案。typescript-require 是未维护的,而 ts-node 是它的替代品。将这个答案留在这里供后代使用。
Found it. The typescript
module is actually like a "main" function; it runs the compiler as soon as the module is loaded. Not very nice design.
找到了。该typescript
模块实际上是像一个“主”函数; 一旦加载模块,它就会运行编译器。不是很好的设计。
I poked at Mocha's acceptance tests, which show how to use a custom compiler for foo
files. They wire it up via the require.extensions
mechanism. I was halfway through writing a module that just calls tsc
on the command line when I realized that somebody must have done this before. So it's very simple:
我研究了 Mocha 的验收测试,它展示了如何对foo
文件使用自定义编译器。他们通过require.extensions
机制将它连接起来。tsc
当我意识到之前一定有人这样做过时,我正在编写一个只在命令行上调用的模块。所以这很简单:
$ npm install typescript-require --save-dev
$ mocha --compilers ts:typescript-require
回答by ToDevAndBeyond
Using the latest version of Mochaand ts-nodeI was getting an Unexpected tokenimport issue. Using the below settings with ts-mochaworked for me:
使用最新版本的Mocha和ts-node,我遇到了意外的令牌导入问题。在ts-mocha 中使用以下设置对我有用:
tsconfig.json
配置文件
{
"files": [
"src/main.ts"
],
"compilerOptions": {
"noImplicitAny": true,
"target": "es2015",
"types": ["mocha"],
"module": "commonjs"
}
}
package.json
包.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"
}
and gulp.js just incase you want to use gulp too
和 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'));