-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
console-reporter.js
181 lines (150 loc) · 4.19 KB
/
console-reporter.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* @flow */
import type {MockData} from './_mock.js';
import ProgressBar from '../../src/reporters/console/progress-bar.js';
import Spinner from '../../src/reporters/console/spinner-progress.js';
import ConsoleReporter from '../../src/reporters/console/console-reporter.js';
import build from './_mock.js';
const getConsoleBuff = build(ConsoleReporter, (data): MockData => data);
const stream = require('stream');
// ensures consistency across environments
require('chalk').enabled = true;
require('chalk').supportsColor = true;
require('chalk').styles.blue.open = '\u001b[34m';
test('ConsoleReporter.step', async () => {
expect(await getConsoleBuff((r) => {
r.step(1, 5, 'foboar');
})).toMatchSnapshot();
});
test('ConsoleReporter.header', async () => {
expect(await getConsoleBuff((r) => {
r.header('foobar', {name: 'yarn', version: '0.0.0'});
})).toMatchSnapshot();
});
test('ConsoleReporter.footer', async () => {
expect(await getConsoleBuff((r) => {
r.footer(false);
})).toMatchSnapshot();
expect(await getConsoleBuff((r) => {
r.footer(true);
})).toMatchSnapshot();
});
test('ConsoleReporter.log', async () => {
expect(await getConsoleBuff((r) => {
r.log('foobar');
})).toMatchSnapshot();
});
test('ConsoleReporter.success', async () => {
expect(await getConsoleBuff((r) => {
r.success('foobar');
})).toMatchSnapshot();
});
test('ConsoleReporter.error', async () => {
expect(await getConsoleBuff((r) => {
r.error('foobar');
})).toMatchSnapshot();
});
test('ConsoleReporter.info', async () => {
expect(await getConsoleBuff((r) => {
r.info('foobar');
})).toMatchSnapshot();
});
test('ConsoleReporter.command', async () => {
expect(await getConsoleBuff((r) => {
r.command('foobar');
})).toMatchSnapshot();
});
test('ConsoleReporter.warn', async () => {
expect(await getConsoleBuff((r) => {
r.warn('foobar');
})).toMatchSnapshot();
});
test('ConsoleReporter.activity', async () => {
expect(await getConsoleBuff(function(r) {
const activity = r.activity();
activity.tick('foo');
activity.end();
})).toMatchSnapshot();
});
test('ConsoleReporter.select', async () => {
expect(await getConsoleBuff(async function (r, streams): Promise<void> {
streams.stdin.on('resume', function() {
streams.stdin.send('1\n', 'ascii');
streams.stdin.end();
});
const res = await r.select('Ayo?', 'Select one', [{
name: 'foo',
value: 'foo',
}, {
name: 'bar',
value: 'bar',
}]);
expect(res, 'foo');
})).toMatchSnapshot();
});
test('ConsoleReporter.progress', async () => {
expect(await getConsoleBuff((r) => {
r.noProgress = false; // we need this to override is-ci when running tests on ci
const tick = r.progress(2);
tick();
jest.runAllTimers();
tick();
})).toMatchSnapshot();
expect(await getConsoleBuff((r) => {
const tick = r.progress(0);
tick();
})).toMatchSnapshot();
expect(await getConsoleBuff((r) => {
r.isTTY = false;
const tick = r.progress(2);
tick();
tick();
})).toMatchSnapshot();
expect(await getConsoleBuff((r) => {
r.noProgress = true;
const tick = r.progress(2);
tick();
})).toMatchSnapshot();
});
test('ProgressBar', () => {
let data = '';
class TestStream extends stream.Writable {
columns: number;
constructor(options) {
super(options);
this.columns = 1000;
}
write(chunk: Buffer | string): boolean {
data += String(chunk);
return true;
}
}
const bar = new ProgressBar(2, new TestStream());
bar.render();
expect(data).toMatchSnapshot();
bar.tick();
bar.render();
expect(data).toMatchSnapshot();
bar.tick();
bar.render();
expect(data).toMatchSnapshot();
});
test('Spinner', () => {
let data = '';
class TestStream extends stream.Writable {
write(chunk: Buffer | string): boolean {
data += String(chunk);
return true;
}
}
const spinner = new Spinner(new TestStream());
spinner.start();
expect(data).toMatchSnapshot();
spinner.setText('foo');
spinner.render();
expect(data).toMatchSnapshot();
spinner.setText('bar');
spinner.render();
expect(data).toMatchSnapshot();
spinner.stop();
expect(data).toMatchSnapshot();
});