How to chain test with mocha with createReadStream function?

I have this function I would like to test with mocha:

exports.readFile = readFile;
function readFile(filepath, startOffset, outputStream){
    var fileSize = fs.statSync(filepath).size;
    var length = fileSize - startOffset;
    console.log(startOffset);
    fs.createReadStream(filepath, 
                    {start: startOffset, end: fileSize}
                                       ).pipe(outputStream);
}

I use the following code to test my function:

var edp = require("edp.js");
    var Buffered = require("buffered-stream");
    var sampleData = 'A small test.';
fs.writeFileSync('./test.txt', sampleData);

var filedata = '';
var smallBufferedStream = new Buffered(20);
smallBufferedStream.on("data", function(data){
        filedata += data;
});

describe('File content redirection', function(){
    describe('reading small file from byte 0', function(){
        it('data should be equal', function(done){
                filedata = '';
                edp.readFile('./test.txt', 0, smallBufferedStream);
                smallBufferedStream.once('end', function(){
                    //sampleData value is "A small test.
                    assert.equal(filedata, sampleData);
                    done();
                });
        });
    });
    describe('reading small file from byte 8', function(){
        it('data should be equal', function(done){
                filedata = '';
                edp.readFile('./test.txt', 8, smallBufferedStream);
                smallBufferedStream.once('end', function(){
                    //sampleData value here is "A small test. 
                    //It should be 'test.'
                    assert.equal(filedata, sampleData.substr(8));
                    done();
                });
        });
    });
});

When I run the mocha command, I obtain the following:

 0
․8
․

✖ 1 of 2 tests failed:

1) File content redirection reading small file from byte 8 data should be equal:

  actual expected

  A small test.

EDIT: the problem comes from the smallBufferedStream which is not reset between tests

This only happens in mocha (I made some test on an external program and this works).

How can I force my buffered stream to reset a new stream each time I call it inside mocha ?

Not sure it is the best way to do this, but I finally found that the problem seems to come from asynchronous calls of the functions.

I solved the problem duplicating my code like this:

describe('reading small file from byte 8', function(){
    it('data should be equal', function(done){
            var smallBufferedStream = new Buffered(20);
            var filedata = '';
            smallBufferedStream.on("data", function(data){
                    filedata += data;
            });
            var fd = edp.readFile(smallTestFile, 8, smallBufferedStream);
            smallBufferedStream.once('end', function(){
                assert.equal(filedata, sampleData.substr(1));

                done();
            });
    });
});

I replace 8 by the value I need to test.

You can use beforeEach in mocha to reset all data between each test. So your code above should work by doing the following:

//Have to initialize these here so they are in scope in the describe functions. 
var filedata = null; 
var smallBufferedStream = null;
describe('File content redirection', function(){
    beforeEach(function(done) {
        filedata = '';
        smallBufferedStream = new Buffered(20);
        smallBufferedStream.on("data", function(data){
            filedata += data;
        });
        done()
    });
    describe('reading small file from byte 0', function(){
        it('data should be equal', function(done){
                filedata = '';
                edp.readFile('./test.txt', 0, smallBufferedStream);
                smallBufferedStream.once('end', function(){
                    //sampleData value is "A small test.
                    assert.equal(filedata, sampleData);
                    done();
                });
        });
    });
    describe('reading small file from byte 8', function(){
        it('data should be equal', function(done){
                filedata = '';
                edp.readFile('./test.txt', 8, smallBufferedStream);
                smallBufferedStream.once('end', function(){
                    //sampleData value here is "A small test. 
                    //It should be 'test.'
                    assert.equal(filedata, sampleData.substr(8));
                    done();
                });
        });
    });
});