PROGRAM:
So I've written a simple TCP server with sockets to write text files with a timestamp as a name whenever a request goes through. (i.e. node server.js in the terminal outputs 2016-01-12 20:02:34 -08:00 as a text file. Works fine.
PROBLEM:
I have written a test using Mocha & Chai to test the number of files in the project directory ONCE the program has run. Theoretically, there would always be one more file than before in the directory because of the new text output. But, for the life of me, I can't figure out what I could be doing wrong. I'm positive it's the way I have written my test (still new to mocha & chai), but reading the docs has gotten me nowhere.
**What I DO know is that when I access the REPL in mocha debug mode and I access the files argument inside fs.readdir (which counts the files in the directory specified), it is returning the number of files existing BEFORE the program has run. I'm baffled.
server.js:
const net = require('net');
const fs = require('fs');
const moment = require('moment');
var port = 3000,
host = '127.0.0.1',
now = moment();
var server = module.exports = exports = net.createServer((socket) => {
console.log('Connected: ' + socket.remoteAddress + ': ' + socket.remotePort);
socket.on('data', function(data) {
console.log('Data: ' + socket.remoteAddress + ': ' + data);
var timeStamp = now.format('YYYY-MM-DD HH:mm:ss Z');
fs.writeFile(timeStamp, data, (err) => {
if (err) return 'Error.'
console.log('File saved.');
});
});
socket.end();
}).listen(port, host);
console.log('Server listening on ' + host + ': ' + port);
test.js:
const expect = require('chai').expect;
const fs = require('fs');
const net = require('net');
const moment = require('moment');
const server = require(__dirname + '/../server.js');
var now = moment();
describe('test tcp server is writing file', () => {
it('should read directory + written file', (done) => {
var client = net.connect({ port: 3000 }, () => {
debugger;
client.write('test');
client.end();
});
fs.readdir('./', (err, files) => {
debugger;
console.log(files);
expect(file.length).to.eql(6)
});
done();
});
});
Any help is appreciated, thank you.
Aucun commentaire:
Enregistrer un commentaire