-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
test.js
151 lines (109 loc) · 4.29 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import process from 'node:process';
import {Buffer} from 'node:buffer';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import Vinyl from 'vinyl';
import {pEvent} from 'p-event';
import size from './index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const runTest = async (stream, predicate) => {
const out = process.stdout.write.bind(process.stdout);
let result = false;
process.stdout.write = string => {
out(string);
if (predicate(string)) {
result = true;
}
};
await pEvent(stream, 'finish');
process.stdout.write = out;
return result;
};
test('show size of files in stream', async t => {
const stream = size({showFiles: true, title: 'test'});
const predicate = string => /fixture2\.js/.test(string);
const result = runTest(stream, predicate);
stream.write(new Vinyl({path: path.join(__dirname, 'fixture.js'), contents: Buffer.alloc(0)}));
stream.write(new Vinyl({path: path.join(__dirname, 'fixture2.js'), contents: Buffer.alloc(1234)}));
stream.write(new Vinyl({path: path.join(__dirname, 'fixture3.js'), contents: Buffer.alloc(1234)}));
stream.end();
t.true(await result);
});
test('no total when `showFiles` enabled and only one file', async t => {
const stream = size({showFiles: true});
const predicate = string => /total/.test(string);
const result = runTest(stream, predicate);
stream.write(new Vinyl({path: path.join(__dirname, 'fixture.js'), contents: Buffer.alloc(1234)}));
stream.end();
t.false(await result);
});
test('has `gzip` option', async t => {
const stream = size({gzip: true});
const predicate = string => /gzipped/.test(string);
const result = runTest(stream, predicate);
stream.write(new Vinyl({path: path.join(__dirname, 'fixture.js'), contents: Buffer.from('unicorn world')}));
stream.end();
t.true(await result);
});
test('has `brotli` option', async t => {
const stream = size({brotli: true});
const predicate = string => /brotli/.test(string);
const result = runTest(stream, predicate);
stream.write(new Vinyl({path: path.join(__dirname, 'fixture.js'), contents: Buffer.from('unicorn world')}));
stream.end();
t.true(await result);
});
test('show `uncompressed`, `gzip`, and `brotli` size', async t => {
const stream = size({uncompressed: true, gzip: true, brotli: true});
const predicate = string => /gzipped.*brotli/.test(string) && /(?:.*\b\d+){3}/.test(string);
const result = runTest(stream, predicate);
stream.write(new Vinyl({path: path.join(__dirname, 'fixture.js'), contents: Buffer.from('unicorn world')}));
stream.end();
t.true(await result);
});
test('no prettified size when `pretty` is false', async t => {
const stream = size({pretty: false});
const predicate = string => /1234 B/.test(string);
const result = runTest(stream, predicate);
stream.write(new Vinyl({path: path.join(__dirname, 'fixture.js'), contents: Buffer.alloc(1234)}));
stream.end();
t.true(await result);
});
test('expose total size', async t => {
const stream = size();
const awaiter = pEvent(stream, 'finish');
stream.write(new Vinyl({path: path.join(__dirname, 'fixture.js'), contents: Buffer.from('unicorn world')}));
stream.end();
await awaiter;
t.is(stream.size, 13);
t.is(stream.prettySize, '13 B');
});
// Stream content tests simplified for brevity. Include your full stream handling logic.
test('handle stream contents', async t => {
const stream = size();
const awaiter = pEvent(stream, 'finish');
stream.write(new Vinyl({path: path.join(__dirname, 'fixture.js'), contents: Buffer.alloc(100)}));
stream.end();
await awaiter;
t.is(stream.size, 100);
t.is(stream.prettySize, '100 B');
});
test('handle stream contents with `gzip` option', async t => {
const stream = size({gzip: true});
const awaiter = pEvent(stream, 'finish');
stream.write(new Vinyl({path: path.join(__dirname, 'fixture.js'), contents: Buffer.from('unicorn world')}));
stream.end();
await awaiter;
t.is(stream.size, 33);
t.is(stream.prettySize, '33 B');
});
test('handle stream contents with `brotli` option', async t => {
const stream = size({brotli: true});
const awaiter = pEvent(stream, 'finish');
stream.write(new Vinyl({path: path.join(__dirname, 'fixture.js'), contents: Buffer.from('unicorn world')}));
stream.end();
await awaiter;
t.is(stream.size, 17);
t.is(stream.prettySize, '17 B');
});