Saturday 12 May 2018

Need help to write mocha test

I built a small module which performs buffering, and once the whole data is received from the server, it emits a message to the client. The code:const EventEmitter = require('events').EventEmitter; class LDJClient extends EventEmitter { constructor(stream) { super(); if (!stream) throw new Error('"stream" can not be null'); let buffer = ""; stream.on('data', data => { buffer += data; let boundary = buffer.indexOf('\n'); while(boundary !== -1) { const input = buffer.substring(0, boundary); buffer = buffer.substring(boundary + 1); try { this.emit('message', JSON.parse(input)); } catch(e) { this.emit('message', { type: e.name, message: e.message }) } boundary = buffer.indexOf('\n'); } }); } static connect(stream) { return new LDJClient(stream); } } module.exports = LDJClient; And the client which exported this module looks like:const netClient = require('net').connect({ port: 6000 });; const ldgClient = require('./lib/ldj-client.js').connect(netClient); ldgClient.on('message', message => { if (message.type === 'watching') { console.log(`Now watching: ${message.file}`); } else if (message.type === 'changed') { const date = new Date(message.timestamp); console.log(`File changed: ${date}`); } else { console.log(`Unrecognized message type: ${message.type}`); } }); I am writing test and improving the code LDJClient as author asked in his book. I need some help to implement the below 2 cases to augment the LDJClient class.What happens if the last data event completes a JSON message, but without the trailing newline?Write a case where the stream object sends a data event containing JSON but no newline, followed by a close event. A real Stream instance will emit a close event when going offline—update LDJClient to listen for close and process the remainder of the buffer.My test looks like so far:const assert = require('assert'); const EventEmitter = require('events').EventEmitter; const LDJClient = require('../lib/ldj-client.js'); describe('LDJClient', () => { let stream = null; let client = null; beforeEach(() => { stream = new EventEmitter(); client = new LDJClient(stream); }); it('should emit a message event from a single data event', done => { client.on('message', message => { assert.deepEqual(message, {foo: 'bar'}); done(); }); stream.emit('data', '{"foo":"bar"}\n'); }); it('should emit a message event from split data events', done => { client.on('message', message => { assert.deepEqual(message, {foo: 'bar'}); done(); }); stream.emit('data', '{"foo":'); process.nextTick(() => stream.emit('data', '"bar"}\n')); }); it('should finish within 50 mili-seconds', done => { setTimeout(done, 40); }).timeout(50); it('should throw error when null is passed to the constructor', done => { stream = null; assert.throws( () => { new LDJClient(stream) }, { name: 'Error', message: '"stream" can not be null' } ); done(); }); it('should emit error message when incoming data is not properly formatted json string', done => { client.on('message', message => { assert.deepEqual(message, { type: 'SyntaxError', message: 'Unexpected end of JSON input' }); done(); }); stream.emit('data', '{"foo":"bar}\n') }); it('should throw error when received data is not JSON', done => { client.on('message', message => { assert.equal('SyntaxError', message.type); done(); }); stream.emit('data', '\n'); }); }); Can anyone help me to solve this?

Submitted May 12, 2018 at 12:02PM by arup_r

No comments:

Post a Comment