gulp:pipe typescript输出到mocha

时间:2016-01-08 04:35:01

标签: typescript gulp mocha

我想将mocha测试添加到TypeScript项目中。在gulpfile.js我管gulp-typescript和gulp-mocha tegether:

gulp.src(['./test/**/*.ts', './typings/tsd.d.ts'])
  .pipe(typescript())
  .pipe(mocha());

Mocha报告错误Error: Cannot find module '.../myproject/test/case1.js'

在Google上搜索,我发现的所有示例都是将typescript转换器输出保存到临时文件中,然后使用mocha运行。我还注意到在gulp-mocha文档中它说“你之前不能有任何插件”:

gulp.task('default', function () {
    return gulp.src('test.js', {read: false})
        // gulp-mocha needs filepaths so you can't have any plugins before it 
        .pipe(mocha({reporter: 'nyan'}));
});

然而,gulp的一个好处是使用流来省略临时文件。如何将转换器输出管道传输到mocha?或者摩卡是不可能的?

2 个答案:

答案 0 :(得分:0)

在使用gulp.dest将typescript编译器输出传递到目标位置之前,您将无法使用mocha的输出文件来运行测试。以下将根据我的测试工作。

gulp.src(['./test/**/*.ts', './typings/tsd.d.ts'])
  .pipe(typescript())
  .pipe(gulp.dest(''))
  .pipe(mocha());

答案 1 :(得分:0)

这是3年前的问题。我发表自己的答案以得出结论。

我的解决方案是不使用任何类似Gulp的构建工具,而是直接使用npm scripts。并且在scripty的帮助下,我可以使用任何脚本语言(例如, Shell脚本或javascript。

这里是一个示例:https://github.com/aleung/node-anyid/blob/master/package.json

参考: Using Npm Scripts as a Build Tool