结合摩卡,打字稿(并观看) [英] Combine mocha, Typescript (and watch)
问题描述
我创建了一个项目,我想在这里使用mocha来测试我的Typescript代码.如果我这样做:
I've created a project where I want to use mocha to test my Typescript code. If I do this:
mocha ts/test --compilers ts:typescript-require
进行更改后,它将失败,并显示以下消息:
After I make a change, it will fail with a message like:
error TS2307: Cannot find module 'mocha'.
这很奇怪,因为如果我只运行tsc
,一切都很好(我拥有所有类型定义,并且正在使用tsconfig.json
文件).我还为mocha
安装了typescript-require
.
This is strange because if I just run tsc
everything is fine (I have all my type definitions and I'm using a tsconfig.json
file). I also installed typescript-require
for mocha
.
但是每次我在此之后运行命令时,它都能正常工作?!?但是,如果我将mocha
设置为监视模式:
But each time I run the command after that, it works?!? But if I put mocha
in watch mode:
mocha -w ts/test --compilers ts:typescript-require
然后它会在第一次运行,但之后会失败.啊!关于如何获得涉及mocha
和Typescript的健壮设置的任何建议,以便我可以进行测试和观看?
Then it works the first time through but fails after that. Ugh! Any suggestions on how to get a robust setup involving mocha
and Typescript so that I can test and watch?
推荐答案
在遇到类似用例( https://github.com/TypeStrong/ts-node ).我需要使用不同的测试运行程序来运行测试,并且编译到不同的目录并不会减少它,因为我也喜欢内联测试夹具.它已扩展为TypeScript的功能完整的节点运行时(包括带有很酷的.type
命令功能的CLI).自述文件中有一个使用Mocha执行它的示例.
I wrote ts-node
after running into a similar use-case (https://github.com/TypeStrong/ts-node). I needed to run tests with different test runners and compiling to a different directory doesn't cut it because I also like inlining the test fixtures. It's been expanded into feature complete node runtime for TypeScript (including a CLI with a cool little .type
command feature). There's an example in the README for executing it with Mocha.
现在全部在内存中,但最终它将通过附加标志进行扩展,以使其适合生产使用(例如,没有运行时开销,只需编译启动).让我知道这对您有何帮助!
It's all in-memory right now, but eventually it will be expanded with additional flags to make it reasonable for production usage (E.g. no runtime overhead, just compilation startup). Let me know how it goes for you!
这篇关于结合摩卡,打字稿(并观看)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!