Browse Source

test(meta): control JS bundle sizes (#17244)

Any drastic increase in production bundle sizes should fail in CI.
version-14
Ankush Menat 3 years ago
committed by GitHub
parent
commit
f40c82d4d8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 2 deletions
  1. +22
    -2
      frappe/tests/test_commands.py

+ 22
- 2
frappe/tests/test_commands.py View File

@@ -12,6 +12,7 @@ import unittest
from contextlib import contextmanager from contextlib import contextmanager
from functools import wraps from functools import wraps
from glob import glob from glob import glob
from pathlib import Path
from typing import List, Optional from typing import List, Optional
from unittest.case import skipIf from unittest.case import skipIf
from unittest.mock import patch from unittest.mock import patch
@@ -31,6 +32,7 @@ from frappe.query_builder.utils import db_type_is
from frappe.tests.test_query_builder import run_only_if 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 import add_to_date, get_bench_path, get_bench_relative_path, now
from frappe.utils.backups import fetch_latest_backups from frappe.utils.backups import fetch_latest_backups
from frappe.utils.jinja_globals import bundled_asset


_result: Optional[Result] = None _result: Optional[Result] = None
TEST_SITE = "commands-site-O4PN2QKA.test" # added random string tag to avoid collisions TEST_SITE = "commands-site-O4PN2QKA.test" # added random string tag to avoid collisions
@@ -692,7 +694,25 @@ class TestSiteMigration(BaseTestCommands):




class TestBenchBuild(BaseTestCommands): 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.exit_code, 0)
self.assertEqual(result.exception, None) 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",
)

Loading…
Cancel
Save