如何运行用TypeScript编写的Mocha测试? [英] How to run Mocha tests written in TypeScript?
问题描述
设置:我有一个用TypeScript编写的Node项目(纯Node,没有浏览器位).我可以使用typescript
模块中的TypeScript编译器(tsc
)来编译代码.到目前为止一切顺利.
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.
但是,我想使用Mocha编写测试,这就是我遇到的麻烦.我尝试了--compilers ts: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:
error TS5023: Unknown compiler option 'compilers'.
mocha
的命令行看起来最终被传递给了tsc
,这显然不好.
It looks like the command line to mocha
ends up being passed to tsc
, which is obviously not good.
推荐答案
对于尝试过 typescript-require 并遇到问题的任何人,您可能都想尝试
For anybody who has tried and had problems with typescript-require you may want to try ts-node.
$ npm install -g ts-node
$ mocha test.ts --require ts-node/register src/**/*.spec.ts
似乎也有关于不赞成使用Typescript-的正在进行的讨论需要支持ts-node.
It also appears that there has been some ongoing discussion about deprecating typescript-require in favor of ts-node.
这篇关于如何运行用TypeScript编写的Mocha测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!