I am developing a CLI tool running on node.js. I am trying to setup my environment for tdd but I have difficulties to do it with gulp.
All my 'standalone' tasks from gulp work. Here they are:
Build task:
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'))
]);
});
Test task:
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);
});
As you can see I have one task which build my typescript files into es5 compatible js. And another task that run my tests with the precedent build.
If I run them alone. It works.
I have tried to add a watch mode:
build:watch:
gulp.task('build:watch', ['build'], function() {
gulp.watch([
PATHS.lib + '/**/*.ts'
], ['build']);
});
test:watch:
gulp.task('test:watch', ['test'], function () {
gulp.watch([
PATHS.lib + '/**/*.ts',
PATHS.test + '/**/*.js'
], ['test']);
});
build:watch works. Every time I edit a typescript file, the build task is triggered and rebuild my project. (It's not really relevant here because the test task trigger build. It's just to say that this watch mode works)
test:watch doesn't work. The first iteration (triggered by the task dependencie) work, but when I edit a typescript file or a test file, I got this error:
{ [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' }
And if you want, here the gulp stack :
[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] ...
Do you have any suggestion?
All right, I find the problem.
I use mockery in my unit tests and I forget to disable it after some tests. I just add in my test files :
afterEach(function() {
mockery.disable();
});
Have a good day. Thomas