test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import fs from 'fs';
  2. import path from 'path';
  3. import test from 'ava';
  4. import Vinyl from 'vinyl';
  5. import stripAnsi from 'strip-ansi';
  6. import pEvent from 'p-event';
  7. import debug from '.';
  8. const logInspect = {
  9. messages: [],
  10. logger(msg) {
  11. logInspect.messages.push(stripAnsi(msg));
  12. },
  13. get notCalled() {
  14. return this.messages.length === 0;
  15. },
  16. get firstMessage() {
  17. return this.messages[0];
  18. },
  19. get lastMessage() {
  20. return this.messages[this.messages.length - 1];
  21. }
  22. };
  23. let file;
  24. test.beforeEach(() => {
  25. logInspect.messages = [];
  26. file = new Vinyl({
  27. cwd: __dirname,
  28. base: __dirname,
  29. path: path.join(__dirname, 'foo.js'),
  30. stat: fs.statSync('test.js'),
  31. contents: Buffer.from('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
  32. });
  33. });
  34. test('output debug info', async t => {
  35. const stream = debug({
  36. logger: logInspect.logger,
  37. title: 'unicorn:'
  38. });
  39. const finish = pEvent(stream, 'finish');
  40. stream.end(file);
  41. await finish;
  42. t.is(logInspect.firstMessage, 'unicorn: foo.js');
  43. });
  44. test('output singular item count', async t => {
  45. const stream = debug({
  46. logger: logInspect.logger,
  47. title: 'unicorn:'
  48. });
  49. const finish = pEvent(stream, 'finish');
  50. stream.end(file);
  51. await finish;
  52. t.is(logInspect.lastMessage, 'unicorn: 1 item');
  53. });
  54. test('output zero item count', async t => {
  55. const stream = debug({
  56. logger: logInspect.logger,
  57. title: 'unicorn:'
  58. });
  59. const finish = pEvent(stream, 'finish');
  60. stream.end();
  61. await finish;
  62. t.is(logInspect.lastMessage, 'unicorn: 0 items');
  63. });
  64. test('output plural item count', async t => {
  65. const stream = debug({
  66. logger: logInspect.logger,
  67. title: 'unicorn:'
  68. });
  69. const finish = pEvent(stream, 'finish');
  70. stream.write(file, () => {
  71. stream.end(file);
  72. });
  73. await finish;
  74. t.is(logInspect.lastMessage, 'unicorn: 2 items');
  75. });
  76. test('do not output file names when `showFiles` is false', async t => {
  77. const stream = debug({
  78. logger: logInspect.logger,
  79. title: 'unicorn:',
  80. showFiles: false
  81. });
  82. const finish = pEvent(stream, 'finish');
  83. stream.write(file, () => {
  84. t.true(logInspect.notCalled);
  85. stream.end();
  86. });
  87. await finish;
  88. t.is(logInspect.lastMessage, 'unicorn: 1 item');
  89. });
  90. test('using the default logger', async t => {
  91. const stream = debug();
  92. const finish = pEvent(stream, 'finish');
  93. stream.end(file);
  94. await finish;
  95. t.pass();
  96. });
  97. test('do not output count when `showCount` is false', async t => {
  98. const stream = debug({
  99. logger: logInspect.logger,
  100. title: 'unicorn:',
  101. showCount: false
  102. });
  103. const finish = pEvent(stream, 'finish');
  104. stream.write(file, () => {
  105. stream.end(file);
  106. });
  107. await finish;
  108. t.not(logInspect.lastMessage, 'unicorn: 1 item');
  109. });