forked from e18e/action-dependency-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle-size_test.ts
More file actions
167 lines (126 loc) · 6.2 KB
/
Copy pathbundle-size_test.ts
File metadata and controls
167 lines (126 loc) · 6.2 KB
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
import {describe, expect, it} from 'vitest';
import {scanForBundleSize} from '../../src/checks/bundle-size.js';
import type {PackInfo} from '../../src/packs.js';
function makePack(packageName: string, size: number): PackInfo {
return {
name: `${packageName}-1.0.0.tgz`,
packageName,
path: `/tmp/${packageName}-1.0.0.tgz`,
size
};
}
describe('scanForBundleSize', () => {
it('should do nothing when no packs are provided', async () => {
const messages: string[] = [];
await scanForBundleSize(messages, [], [], 50000);
expect(messages).toHaveLength(0);
});
it('should report no bundle size change when diff is 0', async () => {
const messages: string[] = [];
const basePacks = [makePack('my-package', 100000)];
const sourcePacks = [makePack('my-package', 100000)];
await scanForBundleSize(messages, basePacks, sourcePacks, 50000);
expect(messages).toHaveLength(0);
});
it('should report no bundle size change with threshold=-1 when diff is 0', async () => {
const messages: string[] = [];
const basePacks = [makePack('my-package', 100000)];
const sourcePacks = [makePack('my-package', 100000)];
await scanForBundleSize(messages, basePacks, sourcePacks, -1);
expect(messages).toHaveLength(1);
expect(messages[0]).toContain('No bundle size changes');
expect(messages).toMatchSnapshot();
});
it('should not report anything when diff is 0 and threshold is not -1', async () => {
const messages: string[] = [];
const basePacks = [makePack('my-package', 100000)];
const sourcePacks = [makePack('my-package', 100000)];
await scanForBundleSize(messages, basePacks, sourcePacks, 50000);
expect(messages).toHaveLength(0);
});
it('should never warn about a pure size decrease, no matter how large', async () => {
const messages: string[] = [];
// sizeChange is signed: (sourceSize - baseSize) = negative for a shrink
// A massive decrease should never cross a positive threshold
const basePacks = [makePack('my-package', 1000000)];
const sourcePacks = [makePack('my-package', 1)];
await scanForBundleSize(messages, basePacks, sourcePacks, 50000);
expect(messages).toHaveLength(0);
});
it('should warn even when net total change is negative due to a large decrease masking an increase', async () => {
const messages: string[] = [];
// pkg-a shrinks by 500 KB, pkg-b grows by 100 KB → net = -400 KB
// Without filtering to increases only, -400 KB < 50 KB threshold → silent (wrong)
const basePacks = [makePack('pkg-a', 500000), makePack('pkg-b', 50000)];
const sourcePacks = [makePack('pkg-a', 0), makePack('pkg-b', 150000)];
await scanForBundleSize(messages, basePacks, sourcePacks, 50000);
expect(messages).toHaveLength(1);
expect(messages[0]).toContain('pkg-b');
expect(messages[0]).not.toContain('pkg-a');
});
it('should sum multiple increases when checking against the threshold', async () => {
const messages: string[] = [];
// Each increase is 30 KB (below the 50 KB threshold individually)
// but combined they are 60 KB (above threshold) → should warn
const basePacks = [makePack('pkg-a', 100000), makePack('pkg-b', 100000)];
const sourcePacks = [makePack('pkg-a', 130000), makePack('pkg-b', 130000)];
await scanForBundleSize(messages, basePacks, sourcePacks, 50000);
expect(messages).toHaveLength(1);
expect(messages[0]).toContain('pkg-a');
expect(messages[0]).toContain('pkg-b');
});
it('should warn about size increase exceeding threshold', async () => {
const messages: string[] = [];
const basePacks = [makePack('my-package', 100000)];
const sourcePacks = [makePack('my-package', 200000)];
await scanForBundleSize(messages, basePacks, sourcePacks, 50000);
expect(messages).toMatchSnapshot();
});
it('should not warn about size increase below threshold', async () => {
const messages: string[] = [];
const basePacks = [makePack('my-package', 100000)];
const sourcePacks = [makePack('my-package', 120000)];
await scanForBundleSize(messages, basePacks, sourcePacks, 50000);
expect(messages).toHaveLength(0);
});
it('should warn about an increase even when a decrease in another package cancels it out in total', async () => {
const messages: string[] = [];
// pkg-a shrinks by 100 KB, pkg-b grows by 100 KB → net = 0, but pkg-b exceeds threshold
const basePacks = [makePack('pkg-a', 200000), makePack('pkg-b', 50000)];
const sourcePacks = [makePack('pkg-a', 100000), makePack('pkg-b', 150000)];
await scanForBundleSize(messages, basePacks, sourcePacks, 50000);
expect(messages).toHaveLength(1);
expect(messages[0]).toContain('pkg-b');
expect(messages[0]).not.toContain('pkg-a');
expect(messages).toMatchSnapshot();
});
it('should not report no-change when changes exist but are below threshold', async () => {
const messages: string[] = [];
const basePacks = [makePack('my-package', 100000)];
const sourcePacks = [makePack('my-package', 120000)];
await scanForBundleSize(messages, basePacks, sourcePacks, 50000);
expect(messages).toHaveLength(0);
});
it('should celebrate size decrease when threshold is -1', async () => {
const messages: string[] = [];
const basePacks = [makePack('my-package', 200000)];
const sourcePacks = [makePack('my-package', 100000)];
await scanForBundleSize(messages, basePacks, sourcePacks, -1);
expect(messages).toMatchSnapshot();
});
it('should show both decreases and increases when threshold is -1', async () => {
const messages: string[] = [];
const basePacks = [makePack('pkg-a', 200000), makePack('pkg-b', 50000)];
const sourcePacks = [makePack('pkg-a', 100000), makePack('pkg-b', 150000)];
await scanForBundleSize(messages, basePacks, sourcePacks, -1);
expect(messages).toHaveLength(1);
expect(messages).toMatchSnapshot();
});
it('should show only increases when threshold is -1 and no decreases', async () => {
const messages: string[] = [];
const basePacks = [makePack('my-package', 100000)];
const sourcePacks = [makePack('my-package', 200000)];
await scanForBundleSize(messages, basePacks, sourcePacks, -1);
expect(messages).toMatchSnapshot();
});
});