-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtest.js
137 lines (112 loc) · 2.77 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {Buffer} from 'node:buffer';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import Vinyl from 'vinyl';
import stripAnsi from 'strip-ansi';
import {pEvent} from 'p-event';
import debug from './index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const logInspect = {
messages: [],
logger(message) {
logInspect.messages.push(stripAnsi(message));
},
get notCalled() {
return this.messages.length === 0;
},
get firstMessage() {
return this.messages[0];
},
get lastMessage() {
return this.messages.at(-1);
},
};
let file;
test.beforeEach(() => {
logInspect.messages = [];
file = new Vinyl({
cwd: __dirname,
base: __dirname,
path: path.join(__dirname, 'foo.js'),
stat: fs.statSync('test.js'),
contents: Buffer.from('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.'),
});
});
test('output debug info', async t => {
const stream = debug({
logger: logInspect.logger,
title: 'unicorn:',
});
const finish = pEvent(stream, 'finish');
stream.end(file);
await finish;
t.is(logInspect.firstMessage, 'unicorn: foo.js');
});
test('output singular item count', async t => {
const stream = debug({
logger: logInspect.logger,
title: 'unicorn:',
});
const finish = pEvent(stream, 'finish');
stream.end(file);
await finish;
t.is(logInspect.lastMessage, 'unicorn: 1 item');
});
test('output zero item count', async t => {
const stream = debug({
logger: logInspect.logger,
title: 'unicorn:',
});
const finish = pEvent(stream, 'finish');
stream.end();
await finish;
t.is(logInspect.lastMessage, 'unicorn: 0 items');
});
test('output plural item count', async t => {
const stream = debug({
logger: logInspect.logger,
title: 'unicorn:',
});
const finish = pEvent(stream, 'finish');
stream.write(file, () => {
stream.end(file);
});
await finish;
t.is(logInspect.lastMessage, 'unicorn: 2 items');
});
test('do not output file names when `showFiles` is false', async t => {
const stream = debug({
logger: logInspect.logger,
title: 'unicorn:',
showFiles: false,
});
const finish = pEvent(stream, 'finish');
stream.write(file, () => {
t.true(logInspect.notCalled);
stream.end();
});
await finish;
t.is(logInspect.lastMessage, 'unicorn: 1 item');
});
test('using the default logger', async t => {
const stream = debug();
const finish = pEvent(stream, 'finish');
stream.end(file);
await finish;
t.pass();
});
test('do not output count when `showCount` is false', async t => {
const stream = debug({
logger: logInspect.logger,
title: 'unicorn:',
showCount: false,
});
const finish = pEvent(stream, 'finish');
stream.write(file, () => {
stream.end(file);
});
await finish;
t.not(logInspect.lastMessage, 'unicorn: 1 item');
});