file-open-test.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * file-open-test.js: Tests for File transport "open" event
  3. *
  4. * (C) 2014 William Wong
  5. * MIT LICENSE
  6. *
  7. */
  8. var assert = require('assert'),
  9. fs = require('fs'),
  10. os = require('os'),
  11. path = require('path'),
  12. vows = require('vows'),
  13. winston = require('../../lib/winston');
  14. vows.describe('winston/transports/file').addBatch({
  15. 'An instance of the File Transport': {
  16. topic: function () {
  17. var callback = this.callback.bind(this),
  18. logPath = path.resolve(__dirname, '../fixtures/logs/file-open-test.log');
  19. try {
  20. fs.unlinkSync(logPath);
  21. } catch (ex) {
  22. if (ex && ex.code !== 'ENOENT') { return callback(ex); }
  23. }
  24. var fileTransport = new (winston.transports.File)({
  25. filename: logPath
  26. }),
  27. logger = new (winston.Logger)({
  28. transports: [fileTransport]
  29. }),
  30. timeline = {};
  31. fileTransport.open(function () {
  32. timeline.open = Date.now();
  33. setTimeout(function () {
  34. logger.info('Hello, World!', function () {
  35. timeline.logged = Date.now();
  36. });
  37. }, 100);
  38. setTimeout(function () {
  39. callback(null, timeline);
  40. }, 1000);
  41. });
  42. },
  43. 'should fire "open" event': function (results) {
  44. assert.isTrue(!!results.open);
  45. },
  46. 'should fire "logged" event': function (results) {
  47. assert.isTrue(!!results.logged);
  48. }
  49. }
  50. }).export(module);