javascript - No output from jasmine-node on FilesizeWatcherSpec - Newbie Alert -
i'm new node.js , jasmine, , javascript experience old , rusty, i'm newbie there too. finished manuel kiessling's book, the node beginner book, , working way through second book, the node craftsman book. i'm stuck on filesizewatcher tutorial. i've been able run earlier tests 1 not working. there similar question on so: no output jasmine-node answer isn't working me.
i'll post code here , can tell me i'm doing wrong.
filesizewatcherspec.js:
'use strict'; var filesizewatcher = require('./filesizewatcher'); var exec = require('child_process').exec; describe('filesizewatcher', function() { var watcher; aftereach(function() { watcher.stop(); }); it('should fire "grew" event when file grew in size', function(done) { var path = './var/tmp/filesizewatcher.test'; exec('rm -f ' + path + ' ; touch ' + path, function() { watcher = new filesizewatcher(path); watcher.on('grew', function(gain) { expect(gain).tobe(5); done(); }); exec('echo "test" > ' + path, function(){}); }); }); it('should fire "shrank" event when file shrank in size', function(done) { var path = './var/tmp/filesizewatcher.test'; exec('rm -f ' + path + ' ; echo "test" > ' + path, function() { watcher = new filesizewather(path); watcher.on('shrank', function(loss) { expect(loss).tobe(3); done(); }); exec('echo "a" > ' + path, function(){}); }); }); it('should fire "error" if path not start', function(done) { var path = 'var/tmp/filesizewatcher.test'; watcher = new filesizewather(path); watcher.on('error', function(err) { expect(err).tobe('path not start slash'); done(); }); }); });
filesizewatcher.js:
'use strict'; var fs = require('fs'); var util = require('util'); var eventemitter = require('events').eventemitter; var filesizewatcher = function (path) { var self = this; if (/^\//.test(path) === false) { process.nexttick(function() { self.emit('error', 'path not start slash'); }); return; } fs.stat(path, function (err, stats) { console.log('stats= ' + stats); self.lastfilesize = stats.size; }); self.interval = setinterval( function () { console.log('we in function()'); fs.stat(path, function (err, stats) { if (stats.size > self.lastfilesize) { self.emit('grew', stats.size - self.lastfilesize); self.lastfilesize = stats.size; } if (stats.size < self.lastfilesize) { self.emit('shrank', self.lastfilesize - stats.size); self.lastfilesize = stats.size; } }, 1000); }); }; util.inherits(filesizewatcher, eventemitter); filesizewatcher.prototype.stop = function () { clearinterval(this.interval); }; module.exports = filesizewatcher;
console output:
c:\users\pdl\projects\nodecraftsman>jasmine-node ./filesizewatcherspec.js c:\users\pdl\projects\nodecraftsman>
other tests run fine:
c:\users\pdl\projects\nodecraftsmantestdrivendevelopment>jasmine-node spec\greetspec.js .. finished in 0.006 seconds 2 tests, 2 assertions, 0 failures, 0 skipped c:\users\pdl\projects\nodecraftsmantestdrivendevelopment>
i added --captureexceptions see if information , got typeerror: self.callbacks.error not function.
my first problem eppilo suggested below, needed use process.nexttick on self.callbacks'error'. mixing async code sync code causes error event fired before error handler registered. made changes , using eventemitter i'm still getting following errors:
if include "." in path: var path = './var/tmp/filesizewatcher.test';
file gets written. otherwise, not.
if file not written, stats= undefined , receive error:
typeerror: cannot read property 'size' of undefined @ c:\users\pdl\projects\nodecraftsman\filesizewatcher.js:19:34 @ fsreqwrap.oncomplete (fs.js:82:15)
if file written, receive error:
error: uncaught, unspecified "error" event. (path not start slash) @ emit (events.js:144:17) @ c:\users\pdl\projects\nodecraftsman\filesizewatcher.js:12:18 @ nexttickcallbackwith0args (node.js:419:9) @ process._tickcallback (node.js:348:13)
of course, it's not supposed start slash. test. when remove --captureexceptions command, still no output.
first of try , run jasmine on verbose mode , capture exceptions:
jasmine-node ./filesizewatcherspec.js --verbose --captureexceptions
link: https://github.com/mhevery/jasmine-node/wiki/command-line-usage
also try make error checking asynchronous:
if (/^\//.test(path) === false) { process.nexttick(function() { self.callbacks['error']('path not start slash'); }); return; }
Comments
Post a Comment