Skip to content

Commit 7c5d779

Browse files
feat: tests for update pr script
1 parent 5ed4bdb commit 7c5d779

File tree

6 files changed

+952
-0
lines changed

6 files changed

+952
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import {
2+
generateBundleSizeSection,
3+
getBundleInfo,
4+
} from '../../../.github/workflows/scripts/utils/bundle';
5+
6+
describe('bundle utils', () => {
7+
describe('generateBundleSizeSection', () => {
8+
it('should generate section for increased bundle size', () => {
9+
const bundleInfo = {
10+
currentSize: 1024 * 1024 * 2, // 2MB
11+
mainSize: 1024 * 1024, // 1MB
12+
diff: 1024 * 1024, // 1MB increase
13+
percent: '100',
14+
};
15+
16+
const result = generateBundleSizeSection(bundleInfo);
17+
expect(result).toContain('Bundle Size: 🔺');
18+
expect(result).toContain('Current: 2.00 MB | Main: 1.00 MB');
19+
expect(result).toContain('Diff: +1.00 MB (100%)');
20+
expect(result).toContain('⚠️ Bundle size increased. Please review.');
21+
});
22+
23+
it('should generate section for decreased bundle size', () => {
24+
const bundleInfo = {
25+
currentSize: 1024 * 1024, // 1MB
26+
mainSize: 1024 * 1024 * 2, // 2MB
27+
diff: -1024 * 1024, // 1MB decrease
28+
percent: '-50',
29+
};
30+
31+
const result = generateBundleSizeSection(bundleInfo);
32+
expect(result).toContain('Bundle Size: 🔽');
33+
expect(result).toContain('Current: 1.00 MB | Main: 2.00 MB');
34+
expect(result).toContain('Diff: 1.00 MB (-50%)');
35+
expect(result).toContain('✅ Bundle size decreased.');
36+
});
37+
38+
it('should generate section for unchanged bundle size', () => {
39+
const bundleInfo = {
40+
currentSize: 1024 * 1024, // 1MB
41+
mainSize: 1024 * 1024, // 1MB
42+
diff: 0,
43+
percent: '0',
44+
};
45+
46+
const result = generateBundleSizeSection(bundleInfo);
47+
expect(result).toContain('Bundle Size: ✅');
48+
expect(result).toContain('Current: 1.00 MB | Main: 1.00 MB');
49+
expect(result).toContain('Diff: 0.00 KB (0%)');
50+
expect(result).toContain('✅ Bundle size unchanged.');
51+
});
52+
53+
it('should handle N/A percent', () => {
54+
const bundleInfo = {
55+
currentSize: 1024 * 1024, // 1MB
56+
mainSize: 0,
57+
diff: 1024 * 1024,
58+
percent: 'N/A',
59+
};
60+
61+
const result = generateBundleSizeSection(bundleInfo);
62+
expect(result).toContain('Bundle Size: ⚠️');
63+
expect(result).toContain('Current: 1.00 MB | Main: 0.00 KB');
64+
expect(result).toContain('Diff: +1.00 MB (N/A)');
65+
expect(result).toContain('⚠️ Unable to calculate change.');
66+
});
67+
});
68+
69+
describe('getBundleInfo', () => {
70+
const originalEnv = process.env;
71+
72+
beforeEach(() => {
73+
jest.resetModules();
74+
process.env = {...originalEnv};
75+
});
76+
77+
afterAll(() => {
78+
process.env = originalEnv;
79+
});
80+
81+
it('should get bundle info from environment variables', () => {
82+
process.env.CURRENT_SIZE = '2097152'; // 2MB
83+
process.env.MAIN_SIZE = '1048576'; // 1MB
84+
process.env.SIZE_DIFF = '1048576'; // 1MB
85+
process.env.SIZE_PERCENT = '100';
86+
87+
const result = getBundleInfo();
88+
expect(result).toEqual({
89+
currentSize: 2097152,
90+
mainSize: 1048576,
91+
diff: 1048576,
92+
percent: '100',
93+
});
94+
});
95+
96+
it('should handle missing environment variables', () => {
97+
delete process.env.CURRENT_SIZE;
98+
delete process.env.MAIN_SIZE;
99+
delete process.env.SIZE_DIFF;
100+
delete process.env.SIZE_PERCENT;
101+
102+
const result = getBundleInfo();
103+
expect(result).toEqual({
104+
currentSize: 0,
105+
mainSize: 0,
106+
diff: 0,
107+
percent: 'N/A',
108+
});
109+
});
110+
});
111+
});
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import {
2+
formatSize,
3+
generateTestChangesSummary,
4+
} from '../../../.github/workflows/scripts/utils/format';
5+
6+
describe('format utils', () => {
7+
describe('formatSize', () => {
8+
it('should format size in KB when less than 1024 bytes', () => {
9+
const size = 512; // 512 bytes
10+
expect(formatSize(size)).toBe('0.50 KB');
11+
});
12+
13+
it('should format size in MB when greater than or equal to 1024 bytes', () => {
14+
const size = 2.5 * 1024; // 2.5 KB -> will be shown in MB
15+
expect(formatSize(size)).toBe('0.00 MB');
16+
});
17+
18+
it('should handle small sizes', () => {
19+
const size = 100; // 100 bytes
20+
expect(formatSize(size)).toBe('0.10 KB');
21+
});
22+
23+
it('should handle zero', () => {
24+
expect(formatSize(0)).toBe('0.00 KB');
25+
});
26+
});
27+
28+
describe('generateTestChangesSummary', () => {
29+
it('should generate summary for new tests only', () => {
30+
const comparison = {
31+
new: ['Test 1 (file1.ts)', 'Test 2 (file2.ts)'],
32+
skipped: [],
33+
deleted: [],
34+
};
35+
36+
const summary = generateTestChangesSummary(comparison);
37+
expect(summary).toContain('✨ New Tests (2)');
38+
expect(summary).toContain('1. Test 1 (file1.ts)');
39+
expect(summary).toContain('2. Test 2 (file2.ts)');
40+
expect(summary).not.toContain('⏭️ Skipped Tests');
41+
expect(summary).not.toContain('🗑️ Deleted Tests');
42+
});
43+
44+
it('should generate summary for skipped tests only', () => {
45+
const comparison = {
46+
new: [],
47+
skipped: ['Test 1 (file1.ts)', 'Test 2 (file2.ts)'],
48+
deleted: [],
49+
};
50+
51+
const summary = generateTestChangesSummary(comparison);
52+
expect(summary).toContain('⏭️ Skipped Tests (2)');
53+
expect(summary).toContain('1. Test 1 (file1.ts)');
54+
expect(summary).toContain('2. Test 2 (file2.ts)');
55+
expect(summary).not.toContain('✨ New Tests');
56+
expect(summary).not.toContain('🗑️ Deleted Tests');
57+
});
58+
59+
it('should generate summary for deleted tests only', () => {
60+
const comparison = {
61+
new: [],
62+
skipped: [],
63+
deleted: ['Test 1 (file1.ts)', 'Test 2 (file2.ts)'],
64+
};
65+
66+
const summary = generateTestChangesSummary(comparison);
67+
expect(summary).toContain('🗑️ Deleted Tests (2)');
68+
expect(summary).toContain('1. Test 1 (file1.ts)');
69+
expect(summary).toContain('2. Test 2 (file2.ts)');
70+
expect(summary).not.toContain('✨ New Tests');
71+
expect(summary).not.toContain('⏭️ Skipped Tests');
72+
});
73+
74+
it('should generate summary for all types of changes', () => {
75+
const comparison = {
76+
new: ['New Test (file1.ts)'],
77+
skipped: ['Skipped Test (file2.ts)'],
78+
deleted: ['Deleted Test (file3.ts)'],
79+
};
80+
81+
const summary = generateTestChangesSummary(comparison);
82+
expect(summary).toContain('✨ New Tests (1)');
83+
expect(summary).toContain('⏭️ Skipped Tests (1)');
84+
expect(summary).toContain('🗑️ Deleted Tests (1)');
85+
expect(summary).toContain('New Test (file1.ts)');
86+
expect(summary).toContain('Skipped Test (file2.ts)');
87+
expect(summary).toContain('Deleted Test (file3.ts)');
88+
});
89+
90+
it('should handle no changes', () => {
91+
const comparison = {
92+
new: [],
93+
skipped: [],
94+
deleted: [],
95+
};
96+
97+
const summary = generateTestChangesSummary(comparison);
98+
expect(summary).toBe('😟 No changes in tests. 😕');
99+
});
100+
});
101+
});
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import fs from 'fs';
2+
3+
import {getTestStatus, readTestResults} from '../../../.github/workflows/scripts/utils/results';
4+
import type {TestResults, TestResultsInfo, TestStatusInfo} from '../types';
5+
6+
jest.mock('fs');
7+
8+
describe('results utils', () => {
9+
describe('readTestResults', () => {
10+
beforeEach(() => {
11+
jest.clearAllMocks();
12+
});
13+
14+
it('should handle non-existent file', () => {
15+
(fs.existsSync as jest.Mock).mockReturnValue(false);
16+
17+
const result = readTestResults('nonexistent.json');
18+
expect(result).toEqual({
19+
total: 0,
20+
passed: 0,
21+
failed: 0,
22+
flaky: 0,
23+
skipped: 0,
24+
tests: [],
25+
});
26+
});
27+
28+
it('should read and process test results correctly', () => {
29+
const mockTestResults: TestResults = {
30+
config: {} as any,
31+
suites: [
32+
{
33+
title: 'Test Suite',
34+
file: 'test.spec.ts',
35+
column: 1,
36+
line: 1,
37+
specs: [
38+
{
39+
title: 'Test 1',
40+
ok: true,
41+
tags: [],
42+
id: '1',
43+
file: 'test.spec.ts',
44+
line: 2,
45+
column: 1,
46+
tests: [
47+
{
48+
timeout: 5000,
49+
annotations: [],
50+
expectedStatus: 'passed',
51+
projectId: '1',
52+
projectName: 'test',
53+
results: [],
54+
status: 'passed',
55+
},
56+
],
57+
},
58+
],
59+
suites: [],
60+
},
61+
],
62+
};
63+
64+
(fs.existsSync as jest.Mock).mockReturnValue(true);
65+
(fs.readFileSync as jest.Mock).mockReturnValue(
66+
JSON.stringify({
67+
...mockTestResults,
68+
stats: {
69+
expected: 5,
70+
unexpected: 2,
71+
flaky: 1,
72+
skipped: 3,
73+
},
74+
}),
75+
);
76+
77+
const result = readTestResults('test-results.json');
78+
expect(result).toEqual({
79+
total: 11,
80+
passed: 5,
81+
failed: 2,
82+
flaky: 1,
83+
skipped: 3,
84+
tests: expect.any(Array),
85+
});
86+
});
87+
});
88+
89+
describe('getTestStatus', () => {
90+
it('should return failed status when there are failures', () => {
91+
const results: TestResultsInfo = {
92+
total: 10,
93+
passed: 8,
94+
failed: 2,
95+
flaky: 0,
96+
skipped: 0,
97+
tests: [],
98+
};
99+
100+
const result = getTestStatus(results) as TestStatusInfo;
101+
expect(result.status).toBe('❌ FAILED');
102+
expect(result.statusColor).toBe('red');
103+
});
104+
105+
it('should return flaky status when there are flaky tests but no failures', () => {
106+
const results: TestResultsInfo = {
107+
total: 10,
108+
passed: 8,
109+
failed: 0,
110+
flaky: 2,
111+
skipped: 0,
112+
tests: [],
113+
};
114+
115+
const result = getTestStatus(results) as TestStatusInfo;
116+
expect(result.status).toBe('⚠️ FLAKY');
117+
expect(result.statusColor).toBe('orange');
118+
});
119+
120+
it('should return passed status when all tests pass', () => {
121+
const results: TestResultsInfo = {
122+
total: 10,
123+
passed: 10,
124+
failed: 0,
125+
flaky: 0,
126+
skipped: 0,
127+
tests: [],
128+
};
129+
130+
const result = getTestStatus(results) as TestStatusInfo;
131+
expect(result.status).toBe('✅ PASSED');
132+
expect(result.statusColor).toBe('green');
133+
});
134+
});
135+
});

0 commit comments

Comments
 (0)