1

This gist shows a code snippet that dumps an object into a CSV file. File writing is done using module csv-write-stream and it returns a promise.

This code works flawlessly in all the Mocha tests that I have made.

When the code is invoked by the main nodejs app (a server-side REPL application involving raw process.stdin and console.log invocations as interaction with the user), the CSV file is created, but it's always empty and no error/warning seems to be thrown.

I have debugged extensively the REPL code with node-debug and the Chrome dev tools: I am sure that the event handlers of the WriteStream are working properly: no 'error', all 'data' seems to be handled, 'close' runs, the promise resolves as expected.

Nevertheless, in the latter case the file is always 0 bytes. I have checked several Q&A's and cannot find anything as specific as this.

My questions are:

  • can I be missing some errors? how can I be sure to track all communications about the file write?
  • in which direction could I intensify my investigation? which other setup could help me isolate the problem?
  • since the problem may be due to the presence of process.stdin in the equation, what is a way to create a simple, light-weight interaction with the user without having to write a webapp?

I am working on Windows 7. Node 6.9.4, npm 3.5.3, csv-write-stream 2.0.0.

Marco Faustinelli
  • 3,734
  • 5
  • 30
  • 49
  • 1
    How do you test it? Are the testing conditions similar to how it's being used in the main app? You're using `process.stdin` in the main app, which is difficult to test with in Mocha, so perhaps there's an issue there? FWIW, [this blogpost](https://glebbahmutov.com/blog/unit-testing-cli-programs/) describes how to mock `process.stdin`. – robertklep Mar 07 '17 at 15:01
  • This is quite interesting. No, my tests are just exercising the code. Will try to include `process.stdin` mocking in the tests. Thank you! – Marco Faustinelli Mar 07 '17 at 15:08

1 Answers1

1

I managed to fix this issue in two ways, either by:

  • resolving the promise upon the 'finish' event of the FileWriteStream rather than on the 'end' event of the CSVWriteStream
  • removing the process.exit() I was using at the end of my operations with process.stdin (this implies that this tutorial page may be in need of some corrections)
Marco Faustinelli
  • 3,734
  • 5
  • 30
  • 49