How to test node sharing feature

I am working on a project that uses node, and we are trying to achieve 100% coverage of our functions. This is the only function that we have not tested, and it is part of another function.

var userInput = ""; req.on("data", function(data){ userInput += data; }); 

How are you going to test this feature? We tried to export the function from another file, but no luck.

I should mention that we use the tape as a test module.

+6
source share
2 answers

You need to trigger this "data" event upon request. For this call to be called.

For example, suppose you have req in your test, you can do something like this (this is Mocha):

 req.trigger('data', 'sampleData'); expect(userInput).to.equal('sampleData'); 
0
source

req.emit('data', {sampleData: 'wrongOrRightSampleDataHere'}) should do this. When creating an instance of an http object or, therefore, req make sure you create a new instance and no other test receives this event.

To be more complete ...

 var assert = require('assert') function test() { var hasBeenCalledAtLeastOnce = false var userInput = ""; // req must be defined somewhere though req.on("data", function(data){ userInput += data; if(hasBeenCalledAtLeastOnce) { assert.equal(userInput, "HelloWorld", "userInput is in fact 'HelloWorld'") } hasBeenCalledAtLeastOnce = true }); req.emit('data', "Hello") req.emit('data', "World") } test() 
0
source

All Articles