Skip to content

Commit

Permalink
test(meta): control JS bundle sizes (frappe#17244)
Browse files Browse the repository at this point in the history
Any drastic increase in production bundle sizes should fail in CI.
  • Loading branch information
ankush authored Jun 20, 2022
1 parent 6350406 commit f40c82d
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions frappe/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from contextlib import contextmanager
from functools import wraps
from glob import glob
from pathlib import Path
from typing import List, Optional
from unittest.case import skipIf
from unittest.mock import patch
Expand All @@ -31,6 +32,7 @@
from frappe.tests.test_query_builder import run_only_if
from frappe.utils import add_to_date, get_bench_path, get_bench_relative_path, now
from frappe.utils.backups import fetch_latest_backups
from frappe.utils.jinja_globals import bundled_asset

_result: Optional[Result] = None
TEST_SITE = "commands-site-O4PN2QKA.test" # added random string tag to avoid collisions
Expand Down Expand Up @@ -692,7 +694,25 @@ def test_migrate_cli(self):


class TestBenchBuild(BaseTestCommands):
def test_build_assets(self):
with cli(frappe.commands.utils.build) as result:
def test_build_assets_size_check(self):
with cli(frappe.commands.utils.build, "--force --production") as result:
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.exception, None)

CURRENT_SIZE = 3.7 # MB
JS_ASSET_THRESHOLD = 0.1

hooks = frappe.get_hooks()
default_bundle = hooks["app_include_js"]

default_bundle_size = 0.0

for chunk in default_bundle:
abs_path = Path.cwd() / frappe.local.sites_path / bundled_asset(chunk)[1:]
default_bundle_size += abs_path.stat().st_size

self.assertLessEqual(
default_bundle_size / (1024 * 1024),
CURRENT_SIZE * (1 + JS_ASSET_THRESHOLD),
f"Default JS bundle size increased by {JS_ASSET_THRESHOLD:.2%} or more",
)

0 comments on commit f40c82d

Please sign in to comment.