Showing posts with label Mocha. Show all posts
Showing posts with label Mocha. Show all posts

Tuesday, March 3, 2020

how to use Chrome DevTools to debug Node.js Code in development?

it should a very interesting topic for debugging the Node.js Code with Chrome.


1. you must have mocha package install in your node.js project.

npm install --save --save-exact mocha@2.47.5

after the installation, you will find this package in the package.json file.

"devDependencies": {
    "chai": "3.5.0",
    "mocha": "2.4.5"
  },

2. set up the test run command in the scripts section

 "scripts": {
    "test:debug": "node --inspect node_modules/mocha/bin/_mocha --watch --no-timeouts"
  },

3. execute npm run test:debug to generate a web socket that Chrome can connect to for debugging.

PS C:\node\databases> npm run test:debug

> databases@1.0.0 test:debug C:\node\databases
> node --inspect node_modules/mocha/bin/_mocha --watch --no-timeouts

Debugger listening on ws://127.0.0.1:9229/433a7d4f-e829-47e0-a4b4-d6f6f87df9f0
For help, see: https://nodejs.org/en/docs/inspector



  parseRDF
    √ should be a function
    √ should parse RDF content


  2 passing (52ms)



4. Launch the Chrome browse and navigate to Chrome//inspect, then click on "Open dedicated DevTools for Node" then click on the inspect link to launch the chrome DevTool.









5. click on the source tab, then navigate to the filesystem. click on the add folder button to add your source code folder. interesting, after you complete this step. you have to close the  DevTools window and click on the inspect link again to force the breakpoint get hitting.









you have to click on the allow button in order to your source code.





6 set a breakpoint on the source code that you want to debug, then open a new terminal in visual studio code to run touch yourJavscript.js.

7. the breakpoint will be hit immediately, you can start to step through your code and debug it.




Monday, March 2, 2020

how to write a test case that emit a close event to process the buffer after the eventemitter going offline?

Today i am continuing my learning Journey with "Node.js 8 The Right Way Practical, Server-Side Javvascript that Scales".

here is the challenge question that required to write a case against eventemitter sends JSON causing the error. follow by the close event. the eventemitter will emit a close event before going offline. the client will listen for the close event, then process the buffer data after error had been encountered.

here is the line to handle the close event that processed the data left in the buffer after throwing an error in the client.

 stream.on('close', ()=>{
            this.emit('message',JSON.parse( buffer));
        })

the following line will set up the test case. stream emit the Json Data without newline, following by emitting a close event.

 it('test case on close event',done=>{
        client.on('message', message=> {
            console.log(message);
            assert.deepEqual(message, {foo: 'bar'});
            done();
        });
        stream.emit('data','{"foo": "bar"}');
        stream.emit('close');
    });

LDJClient.js

'use strict';
const EventEmitter=require('events').EventEmitter;

class LDJClient extends EventEmitter {
    constructor(stream){
        super();
        let buffer='';
        stream.on('data',data=>{
            console.log(data);
            buffer +=data;
            let boundary=buffer.indexOf('\n');
            while(boundary !=-1){
                const input=buffer.substring(0, boundary);
                buffer=buffer.substring(boundary+1);
                this.emit('message', JSON.parse(input));
                boundary=buffer.indexOf('\n');
            }
        });
        stream.on('close', ()=>{
            this.emit('message',JSON.parse( buffer));
        })
    }
    static connect(stream){
        return new LDJClient(stream);
    }
}

module.exports=LDJClient;

test-ldj-client.js

'use strict';
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 for a single data event', done=> {
        client.on('message', message=> {
            assert.deepEqual(message, {foo: 'bar'});
            done();
        });
        stream.emit('data','{"foo": "bar"}\n');
        //stream.emit('data','{"foo":');
        //process.nextTick(()=> stream.emit('data', '"bar"}\n'));
    });

    it('test case on close event',done=>{
        client.on('message', message=> {
            console.log(message);
            assert.deepEqual(message, {foo: 'bar'});
            done();
        });
        stream.emit('data','{"foo": "bar"}');
        stream.emit('close');
    });
});


the output of test case is succeed. cheers