gulp:通过管道将打字稿输出到 mocha

gulp: pipe typescript output to mocha

我想将 mocha 测试添加到 TypeScript 项目中。在 gulpfile.js 中,我将 gulp-typescript 和 gulp-mocha 连接在一起:

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 文档中注意到它说 "you can't have any plugins before it":

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?或者摩卡不可能?

除非您使用 gulp.dest 将 typescript 编译器输出通过管道传输到目标位置,否则您将无法获得可用于 mocha 的输出文件以 运行 您的测试。以下将根据我的测试工作。

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

3年前的问题了。我post自己的回答来总结一下。

我的解决方案是不使用任何像 Gulp 这样的构建工具,而是使用 npm scripts directly. And also with the help of scripty 我可以用任何脚本语言编写构建脚本,例如shell 脚本或 javascript.

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

参考: Using Npm Scripts as a Build Tool