1 | const gulp = require('gulp');
|
2 | const size = require('.');
|
3 | const Vinyl = require('vinyl');
|
4 | const { Readable, Writable } = require('stream');
|
5 | let stdoutSpy;
|
6 |
|
7 | beforeEach(() => {
|
8 | stdoutSpy = jest.spyOn(process.stdout, 'write');
|
9 | });
|
10 |
|
11 | afterEach(() => {
|
12 | stdoutSpy.mockRestore();
|
13 | });
|
14 |
|
15 | describe('stream-size', () => {
|
16 | it('should work with gulp and a real file', (done) => {
|
17 | gulp
|
18 | .src('test.txt')
|
19 | .pipe(size())
|
20 | .pipe(gulp.dest('./'))
|
21 | .on('end', () => {
|
22 | expect(stdoutSpy).lastCalledWith(
|
23 | expect.stringContaining('test.txt: 27 B'),
|
24 | expect.anything()
|
25 | );
|
26 | done();
|
27 | });
|
28 | });
|
29 |
|
30 | it('should work with a Vinyl', done => {
|
31 | const stream = size();
|
32 |
|
33 | stream.write(new Vinyl({
|
34 | path: 'example.js',
|
35 | contents: Buffer.alloc(1234)
|
36 | }));
|
37 |
|
38 | stream.on('finish', () => {
|
39 | expect(stdoutSpy).lastCalledWith(
|
40 | expect.stringContaining('example.js: 1.23 kB'),
|
41 | expect.anything()
|
42 | );
|
43 | done();
|
44 | });
|
45 |
|
46 | stream.end();
|
47 | });
|
48 |
|
49 | it('should log gzipped size with gzip: true', done => {
|
50 | const stream = size({gzip: true});
|
51 |
|
52 | stream.write(new Vinyl({
|
53 | path: 'example.js',
|
54 | contents: Buffer.alloc(1234)
|
55 | }));
|
56 |
|
57 | stream.on('finish', () => {
|
58 | expect(stdoutSpy).lastCalledWith(
|
59 | expect.stringContaining('example.js: 1.23 kB (gzipped: 30 B)'),
|
60 | expect.anything()
|
61 | );
|
62 | done();
|
63 | });
|
64 |
|
65 | stream.end();
|
66 | });
|
67 |
|
68 | it('should log bytes rather than using filesize with bytes: true', done => {
|
69 | const stream = size({bytes: true});
|
70 |
|
71 | stream.write(new Vinyl({
|
72 | path: 'example.js',
|
73 | contents: Buffer.alloc(1234)
|
74 | }));
|
75 |
|
76 | stream.on('finish', () => {
|
77 | expect(stdoutSpy).lastCalledWith(
|
78 | expect.stringContaining('example.js: 1234 B'),
|
79 | expect.anything()
|
80 | );
|
81 | done();
|
82 | });
|
83 |
|
84 | stream.end();
|
85 | });
|
86 |
|
87 | it('should pass options to the filesize package', done => {
|
88 | const stream = size({base: 2});
|
89 |
|
90 | stream.write(new Vinyl({
|
91 | path: 'example.js',
|
92 | contents: Buffer.alloc(1234)
|
93 | }));
|
94 |
|
95 | stream.on('finish', () => {
|
96 | expect(stdoutSpy).lastCalledWith(
|
97 | expect.stringContaining('example.js: 1.21 KiB'),
|
98 | expect.anything()
|
99 | );
|
100 | done();
|
101 | });
|
102 |
|
103 | stream.end();
|
104 | });
|
105 |
|
106 | it('should run the callback function with it\'s parameter', done => {
|
107 | const stream = size({}, size => process.stdout.write(`${size}`));
|
108 |
|
109 | stream.write(new Vinyl({
|
110 | path: 'example.js',
|
111 | contents: Buffer.alloc(1234)
|
112 | }));
|
113 |
|
114 | stream.on('finish', () => {
|
115 | expect(stdoutSpy).lastCalledWith('1.23 kB');
|
116 | done();
|
117 | });
|
118 |
|
119 | stream.end();
|
120 | });
|
121 |
|
122 | it('should have filename, size, gzip props on the callback param', done => {
|
123 | const stream = size({}, info => process.stdout.write(
|
124 | `${info.filename}: ${info.size} (gzipped: ${info.gzip})`
|
125 | ));
|
126 |
|
127 | stream.write(new Vinyl({
|
128 | path: 'example.js',
|
129 | contents: Buffer.alloc(1234)
|
130 | }));
|
131 |
|
132 | stream.on('finish', () => {
|
133 | expect(stdoutSpy).lastCalledWith('example.js: 1.23 kB (gzipped: 30 B)');
|
134 | done();
|
135 | });
|
136 |
|
137 | stream.end();
|
138 | });
|
139 |
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 | it('should run transformCallback() (even if no stream or buffer) ', done => {
|
146 | const stream = size();
|
147 |
|
148 | stream.write({isStream: () => false, isBuffer: () => false});
|
149 |
|
150 | stream.on('finish', () => {
|
151 | expect(stdoutSpy).not.toHaveBeenCalled();
|
152 | done();
|
153 | });
|
154 |
|
155 | stream.end();
|
156 | });
|
157 | });
|