36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import { mkdtempSync, writeFileSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { describe, expect, it } from 'vitest'
|
|
// @ts-expect-error — .mjs без типов
|
|
import { checkBudget } from '../../../scripts/ci/web-budget.mjs'
|
|
|
|
function dist(files: Record<string, string>): string {
|
|
const dir = mkdtempSync(join(tmpdir(), 'web-budget-'))
|
|
for (const [name, body] of Object.entries(files)) {
|
|
writeFileSync(join(dir, name), body)
|
|
}
|
|
return dir
|
|
}
|
|
|
|
describe('web-budget', () => {
|
|
it('в бюджете → без ошибок', () => {
|
|
const dir = dist({ 'index-abc.js': 'a'.repeat(100), 'x-1.js': 'b'.repeat(100) })
|
|
const { errors, sizes } = checkBudget(dir, { entryGz: 1000, totalGz: 2000 })
|
|
expect(errors).toEqual([])
|
|
expect(sizes.entryGz).toBeGreaterThan(0)
|
|
})
|
|
|
|
it('превышение entry и total → ошибки', () => {
|
|
const noise = Array.from({ length: 5000 }, (_, i) => `v${i}=${Math.random()}`).join(';')
|
|
const dir = dist({ 'index-abc.js': noise })
|
|
const { errors } = checkBudget(dir, { entryGz: 10, totalGz: 10 })
|
|
expect(errors).toHaveLength(2)
|
|
})
|
|
|
|
it('нет entry-чанка → ошибка', () => {
|
|
const dir = dist({ 'other.js': 'x' })
|
|
expect(checkBudget(dir).errors[0]).toMatch(/entry chunk/)
|
|
})
|
|
})
|