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 ?