|
1 | 1 | import {getBundleSize, formatBundleSize} from './bundle-size.js' |
2 | | -import {describe, expect, test, vi} from 'vitest' |
3 | | -import {readFile} from '@shopify/cli-kit/node/fs' |
| 2 | +import {describe, expect, test} from 'vitest' |
| 3 | +import {inTemporaryDirectory, writeFile} from '@shopify/cli-kit/node/fs' |
| 4 | +import {joinPath} from '@shopify/cli-kit/node/path' |
4 | 5 | import {deflate} from 'node:zlib' |
5 | 6 | import {promisify} from 'node:util' |
6 | 7 |
|
7 | 8 | const deflateAsync = promisify(deflate) |
8 | 9 |
|
9 | | -vi.mock('@shopify/cli-kit/node/fs') |
10 | | - |
11 | 10 | describe('getBundleSize', () => { |
12 | 11 | test('returns raw and compressed sizes', async () => { |
13 | | - // Given |
14 | | - const content = 'a'.repeat(10000) |
15 | | - vi.mocked(readFile).mockResolvedValue(content as any) |
16 | | - |
17 | | - // When |
18 | | - const result = await getBundleSize('/some/path.js') |
19 | | - |
20 | | - // Then |
21 | | - expect(result.rawBytes).toBe(10000) |
22 | | - expect(result.compressedBytes).toBe((await deflateAsync(Buffer.from(content))).byteLength) |
23 | | - expect(result.compressedBytes).toBeLessThan(result.rawBytes) |
| 12 | + await inTemporaryDirectory(async (tmpDir) => { |
| 13 | + // Given |
| 14 | + const content = 'a'.repeat(10000) |
| 15 | + const filePath = joinPath(tmpDir, 'bundle.js') |
| 16 | + await writeFile(filePath, content) |
| 17 | + |
| 18 | + // When |
| 19 | + const result = await getBundleSize(filePath) |
| 20 | + |
| 21 | + // Then |
| 22 | + expect(result.rawBytes).toBe(10000) |
| 23 | + expect(result.compressedBytes).toBe((await deflateAsync(Buffer.from(content))).byteLength) |
| 24 | + expect(result.compressedBytes).toBeLessThan(result.rawBytes) |
| 25 | + }) |
24 | 26 | }) |
25 | 27 |
|
26 | 28 | test('compressed size uses deflate to match the backend (Ruby Zlib::Deflate.deflate)', async () => { |
27 | | - // Given |
28 | | - const content = JSON.stringify({key: 'value', nested: {array: [1, 2, 3]}}) |
29 | | - vi.mocked(readFile).mockResolvedValue(content as any) |
30 | | - |
31 | | - // When |
32 | | - const result = await getBundleSize('/some/path.js') |
33 | | - |
34 | | - // Then |
35 | | - const expectedCompressed = (await deflateAsync(Buffer.from(content))).byteLength |
36 | | - expect(result.compressedBytes).toBe(expectedCompressed) |
| 29 | + await inTemporaryDirectory(async (tmpDir) => { |
| 30 | + // Given |
| 31 | + const content = JSON.stringify({key: 'value', nested: {array: [1, 2, 3]}}) |
| 32 | + const filePath = joinPath(tmpDir, 'bundle.js') |
| 33 | + await writeFile(filePath, content) |
| 34 | + |
| 35 | + // When |
| 36 | + const result = await getBundleSize(filePath) |
| 37 | + |
| 38 | + // Then |
| 39 | + const expectedCompressed = (await deflateAsync(Buffer.from(content))).byteLength |
| 40 | + expect(result.compressedBytes).toBe(expectedCompressed) |
| 41 | + }) |
37 | 42 | }) |
38 | 43 | }) |
39 | 44 |
|
40 | 45 | describe('formatBundleSize', () => { |
41 | 46 | test('returns formatted size string with raw and compressed sizes', async () => { |
42 | | - // Given |
43 | | - const content = 'x'.repeat(50000) |
44 | | - const compressedSize = (await deflateAsync(Buffer.from(content))).byteLength |
45 | | - vi.mocked(readFile).mockResolvedValue(content as any) |
46 | | - |
47 | | - // When |
48 | | - const result = await formatBundleSize('/some/path.js') |
49 | | - |
50 | | - // Then |
51 | | - const expectedRaw = (50000 / 1024).toFixed(1) |
52 | | - const expectedCompressed = (compressedSize / 1024).toFixed(1) |
53 | | - expect(result).toBe(` (${expectedRaw} KB original, ~${expectedCompressed} KB compressed)`) |
| 47 | + await inTemporaryDirectory(async (tmpDir) => { |
| 48 | + // Given |
| 49 | + const content = 'x'.repeat(50000) |
| 50 | + const filePath = joinPath(tmpDir, 'bundle.js') |
| 51 | + await writeFile(filePath, content) |
| 52 | + const compressedSize = (await deflateAsync(Buffer.from(content))).byteLength |
| 53 | + |
| 54 | + // When |
| 55 | + const result = await formatBundleSize(filePath) |
| 56 | + |
| 57 | + // Then |
| 58 | + const expectedRaw = (50000 / 1024).toFixed(1) |
| 59 | + const expectedCompressed = (compressedSize / 1024).toFixed(1) |
| 60 | + expect(result).toBe(` (${expectedRaw} KB original, ~${expectedCompressed} KB compressed)`) |
| 61 | + }) |
54 | 62 | }) |
55 | 63 |
|
56 | 64 | test('formats MB for large files', async () => { |
57 | | - // Given |
58 | | - const content = 'a'.repeat(2 * 1024 * 1024) |
59 | | - const compressedSize = (await deflateAsync(Buffer.from(content))).byteLength |
60 | | - vi.mocked(readFile).mockResolvedValue(content as any) |
61 | | - |
62 | | - // When |
63 | | - const result = await formatBundleSize('/some/path.js') |
64 | | - |
65 | | - // Then |
66 | | - const expectedRaw = (Buffer.byteLength(content) / (1024 * 1024)).toFixed(2) |
67 | | - const expectedCompressed = (compressedSize / 1024).toFixed(1) |
68 | | - expect(result).toBe(` (${expectedRaw} MB original, ~${expectedCompressed} KB compressed)`) |
| 65 | + await inTemporaryDirectory(async (tmpDir) => { |
| 66 | + // Given |
| 67 | + const content = 'a'.repeat(2 * 1024 * 1024) |
| 68 | + const filePath = joinPath(tmpDir, 'bundle.js') |
| 69 | + await writeFile(filePath, content) |
| 70 | + const compressedSize = (await deflateAsync(Buffer.from(content))).byteLength |
| 71 | + |
| 72 | + // When |
| 73 | + const result = await formatBundleSize(filePath) |
| 74 | + |
| 75 | + // Then |
| 76 | + const expectedRaw = (Buffer.byteLength(content) / (1024 * 1024)).toFixed(2) |
| 77 | + const expectedCompressed = (compressedSize / 1024).toFixed(1) |
| 78 | + expect(result).toBe(` (${expectedRaw} MB original, ~${expectedCompressed} KB compressed)`) |
| 79 | + }) |
69 | 80 | }) |
70 | 81 |
|
71 | 82 | test('returns empty string on error', async () => { |
72 | | - // Given |
73 | | - vi.mocked(readFile).mockRejectedValue(new Error('file not found')) |
74 | | - |
75 | 83 | // When |
76 | 84 | const result = await formatBundleSize('/missing/path.js') |
77 | 85 |
|
|
0 commit comments