Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 60 additions & 52 deletions packages/app/src/cli/services/build/bundle-size.test.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,85 @@
import {getBundleSize, formatBundleSize} from './bundle-size.js'
import {describe, expect, test, vi} from 'vitest'
import {readFile} from '@shopify/cli-kit/node/fs'
import {describe, expect, test} from 'vitest'
import {inTemporaryDirectory, writeFile} from '@shopify/cli-kit/node/fs'
import {joinPath} from '@shopify/cli-kit/node/path'
import {deflate} from 'node:zlib'
import {promisify} from 'node:util'

const deflateAsync = promisify(deflate)

vi.mock('@shopify/cli-kit/node/fs')

describe('getBundleSize', () => {
test('returns raw and compressed sizes', async () => {
// Given
const content = 'a'.repeat(10000)
vi.mocked(readFile).mockResolvedValue(content as any)

// When
const result = await getBundleSize('/some/path.js')

// Then
expect(result.rawBytes).toBe(10000)
expect(result.compressedBytes).toBe((await deflateAsync(Buffer.from(content))).byteLength)
expect(result.compressedBytes).toBeLessThan(result.rawBytes)
await inTemporaryDirectory(async (tmpDir) => {
// Given
const content = 'a'.repeat(10000)
const filePath = joinPath(tmpDir, 'bundle.js')
await writeFile(filePath, content)

// When
const result = await getBundleSize(filePath)

// Then
expect(result.rawBytes).toBe(10000)
expect(result.compressedBytes).toBe((await deflateAsync(Buffer.from(content))).byteLength)
expect(result.compressedBytes).toBeLessThan(result.rawBytes)
})
})

test('compressed size uses deflate to match the backend (Ruby Zlib::Deflate.deflate)', async () => {
// Given
const content = JSON.stringify({key: 'value', nested: {array: [1, 2, 3]}})
vi.mocked(readFile).mockResolvedValue(content as any)

// When
const result = await getBundleSize('/some/path.js')

// Then
const expectedCompressed = (await deflateAsync(Buffer.from(content))).byteLength
expect(result.compressedBytes).toBe(expectedCompressed)
await inTemporaryDirectory(async (tmpDir) => {
// Given
const content = JSON.stringify({key: 'value', nested: {array: [1, 2, 3]}})
const filePath = joinPath(tmpDir, 'bundle.js')
await writeFile(filePath, content)

// When
const result = await getBundleSize(filePath)

// Then
const expectedCompressed = (await deflateAsync(Buffer.from(content))).byteLength
expect(result.compressedBytes).toBe(expectedCompressed)
})
})
})

describe('formatBundleSize', () => {
test('returns formatted size string with raw and compressed sizes', async () => {
// Given
const content = 'x'.repeat(50000)
const compressedSize = (await deflateAsync(Buffer.from(content))).byteLength
vi.mocked(readFile).mockResolvedValue(content as any)

// When
const result = await formatBundleSize('/some/path.js')

// Then
const expectedRaw = (50000 / 1024).toFixed(1)
const expectedCompressed = (compressedSize / 1024).toFixed(1)
expect(result).toBe(` (${expectedRaw} KB original, ~${expectedCompressed} KB compressed)`)
await inTemporaryDirectory(async (tmpDir) => {
// Given
const content = 'x'.repeat(50000)
const filePath = joinPath(tmpDir, 'bundle.js')
await writeFile(filePath, content)
const compressedSize = (await deflateAsync(Buffer.from(content))).byteLength

// When
const result = await formatBundleSize(filePath)

// Then
const expectedRaw = (50000 / 1024).toFixed(1)
const expectedCompressed = (compressedSize / 1024).toFixed(1)
expect(result).toBe(` (${expectedRaw} KB original, ~${expectedCompressed} KB compressed)`)
})
})

test('formats MB for large files', async () => {
// Given
const content = 'a'.repeat(2 * 1024 * 1024)
const compressedSize = (await deflateAsync(Buffer.from(content))).byteLength
vi.mocked(readFile).mockResolvedValue(content as any)

// When
const result = await formatBundleSize('/some/path.js')

// Then
const expectedRaw = (Buffer.byteLength(content) / (1024 * 1024)).toFixed(2)
const expectedCompressed = (compressedSize / 1024).toFixed(1)
expect(result).toBe(` (${expectedRaw} MB original, ~${expectedCompressed} KB compressed)`)
await inTemporaryDirectory(async (tmpDir) => {
// Given
const content = 'a'.repeat(2 * 1024 * 1024)
const filePath = joinPath(tmpDir, 'bundle.js')
await writeFile(filePath, content)
const compressedSize = (await deflateAsync(Buffer.from(content))).byteLength

// When
const result = await formatBundleSize(filePath)

// Then
const expectedRaw = (Buffer.byteLength(content) / (1024 * 1024)).toFixed(2)
const expectedCompressed = (compressedSize / 1024).toFixed(1)
expect(result).toBe(` (${expectedRaw} MB original, ~${expectedCompressed} KB compressed)`)
})
})

test('returns empty string on error', async () => {
// Given
vi.mocked(readFile).mockRejectedValue(new Error('file not found'))

// When
const result = await formatBundleSize('/missing/path.js')

Expand Down
Loading