Gulp 个任务单独工作但不在监视模式下

Gulp tasks working alone but not in watch mode

我正在 运行ning node.js 上开发 CLI 工具。我正在尝试为 tdd 设置我的环境,但我很难用 gulp.

我 gulp 的所有 'standalone' 任务都有效。 他们在这里:

构建任务:

var tsProject = ts.createProject({
    declarationFiles: true,
    noEmitOnError: true,
    module: 'commonjs',
    target: 'es5',
    sortOutput: true,
    typescript: require('typescript') //usefull during typescript 1.5 alpha
});

gulp.task('build', function() {
    var tsResult = gulp.src([
       PATHS.lib + '/**/*.ts'
    ])
    .pipe(sourcemaps.init())
    .pipe(ts(tsProject));

    return merge([ // Merge the two output streams, so this task is finished when the IO of both operations are done.
        tsResult.dts.pipe(gulp.dest(PATHS.build + '/definitions')),
        tsResult.js
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(PATHS.build + '/js'))
    ]);
});

测试任务:

gulp.task('test', ['build'], function () {
    return gulp.src(PATHS.test + '/**/*.js', {read: false})
    .pipe(mocha({
        reporter: 'spec',
        globals: {
            should: require('should'),
            sinon: require('sinon'),
            mockery: require('mockery')
        }
    }))
    .on('error', gutil.log);
});

如您所见,我有一项任务是将我的打字稿文件构建成兼容 es5 的 js。 运行 我使用先例构建测试的另一个任务。

如果我运行他们一个人。有效。

我试过添加手表模式:

构建:观看:

gulp.task('build:watch', ['build'], function() {
   gulp.watch([
       PATHS.lib + '/**/*.ts'
   ], ['build']);
});

测试:观看:

gulp.task('test:watch', ['test'], function () {
    gulp.watch([
       PATHS.lib + '/**/*.ts',
       PATHS.test + '/**/*.js'
    ], ['test']);
});

build:watch 有效。每次我编辑打字稿文件时,都会触发构建任务并重建我的项目。 (这里其实不相关,因为测试任务会触发build,只是说这个watch mode有效)

测试:观察 不起作用。第一次迭代(由任务依赖性触发)工作,但是当我编辑打字稿文件或测试文件时,出现此错误:

{ [TypeError: Cannot read property 'defaultEncoding' of undefined]
  domain:
   { domain: null,
     _events: { error: [Function: handleException] },
     _maxListeners: undefined,
     members: [] },
  domainThrown: true,
  name: 'TypeError',
  message: 'Cannot read property \'defaultEncoding\' of undefined',
  stack: 'TypeError: Cannot read property \'defaultEncoding\' of undefined\n    at DestroyableTransform.Writable.write (/Users/thomashourlier/Documents/Work/jsProjects/hawker/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:186:21)\n    at Glob.<anonymous> (/Users/thomashourlier/Documents/Work/jsProjects/hawker/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-stream/index.js:44:14)\n    at Glob.emit (events.js:107:17)\n    at Glob._emitMatch (/Users/thomashourlier/Documents/Work/jsProjects/hawker/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-stream/node_modules/glob/glob.js:457:8)\n    at Glob._processReaddir2 (/Users/thomashourlier/Documents/Work/jsProjects/hawker/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-stream/node_modules/glob/glob.js:405:12)\n    at /Users/thomashourlier/Documents/Work/jsProjects/hawker/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-stream/node_modules/glob/glob.js:345:17\n    at RES (/Users/thomashourlier/Documents/Work/jsProjects/hawker/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-stream/node_modules/glob/node_modules/inflight/inflight.js:23:14)\n    at /Users/thomashourlier/Documents/Work/jsProjects/hawker/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-stream/node_modules/glob/node_modules/inflight/inflight.js:30:13\n    at process._tickDomainCallback (node.js:381:11)',
  showStack: true,
  showProperties: true,
  plugin: 'gulp-mocha' }

如果需要,这里是 gulp 堆栈:

[10:58:24] Using gulpfile ~/Documents/Work/jsProjects/hawker/gulpfile.js
[10:58:24] Starting 'build'...
[10:58:25] Finished 'build' after 1.01 s
[10:58:25] Starting 'test'...


  Hawker
    ✓ should get the Logger
    ✓ should get the Parser
    ✓ should define a file loader
    ✓ should define a url loader
    ✓ should launch hawker

  FileLoader
    ✓ should be defined
    ✓ should get a configuration file

  Parser
    ✓ should be defined
    ✓ should parse configuration file


  9 passing (24ms)

[10:58:26] Finished 'test' after 98 ms
[10:58:26] Starting 'test:watch'...
[10:58:26] Finished 'test:watch' after 14 ms
[10:58:28] Starting 'build'...
[10:58:28] { [TypeError: Cannot read property 'defaultEncoding' of undefined] ...

你有什么建议吗?

好的,我找到问题了

我在我的单元测试中使用了 mockery,但我在一些测试后忘记禁用它。我只是添加了我的测试文件:

 afterEach(function() {
    mockery.disable();
 });

祝你有美好的一天。 托马斯