UNPKG

@hint/hint-performance-budget

Version:

hint that that checks if a page passes a set performance budget

113 lines (112 loc) 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const zlib_1 = require("zlib"); const utils_create_server_1 = require("@hint/utils-create-server"); const utils_tests_helpers_1 = require("@hint/utils-tests-helpers"); const utils_types_1 = require("@hint/utils-types"); const hintPath = (0, utils_tests_helpers_1.getHintPath)(__filename); const image = (0, fs_1.readFileSync)(`${__dirname}/fixtures/image.png`); const imageResponse = { content: image, header: { 'Content-Type': 'image/png' } }; const generateBody = (imageCount) => { let html = ''; for (let i = 0; i < imageCount; i++) { html += `<img src="/image-${i}.png">`; } return html; }; const generateServerConfig = (imageCount, redirects = false) => { const serverConfig = {}; for (let i = 0; i < imageCount; i++) { if (redirects) { serverConfig[`/image-${i}.png`] = { content: `/image${i}.png`, status: 302 }; serverConfig[`/image${i}.png`] = imageResponse; } else { serverConfig[`/image-${i}.png`] = imageResponse; } } const html = (0, utils_create_server_1.generateHTMLPage)('', generateBody(imageCount)); const compressed = (0, zlib_1.gzipSync)(Buffer.from(html, 'utf-8')); serverConfig['/'] = { content: compressed, headers: { 'content-encoding': 'gzip', 'content-type': 'text/html' } }; return serverConfig; }; const tests = [ { name: 'Plain page loads fast enough', serverConfig: (0, utils_create_server_1.generateHTMLPage)() }, { name: `Page with 2 images loads under 5s on 3GFast`, reports: [{ message: `To load all the resources on a 3GFast network, it will take about 4.6s in optimal conditions (that is -0.4s less than the 5s target).`, severity: utils_types_1.Severity.hint }], serverConfig: generateServerConfig(2) }, { name: `Page with 3 images and redirects doesn't load under 5s on 3GFast`, reports: [{ message: `To load all the resources on a 3GFast network, it will take about 7.1s in optimal conditions (that is 2.1s more than the 5s target).`, severity: utils_types_1.Severity.warning }], serverConfig: generateServerConfig(3, true) }, { name: `Page with 10 images doesn't load under 5s on 3GFast`, reports: [{ message: `To load all the resources on a 3GFast network, it will take about 22.4s in optimal conditions (that is 17.4s more than the 5s target).`, severity: utils_types_1.Severity.error }], serverConfig: generateServerConfig(10, true) } ]; const loadTimeTests = [ { name: 'Plain page loads under 1s', serverConfig: (0, utils_create_server_1.generateHTMLPage)() }, { name: `Page with 1 image doesn't load under 1s on 3GFast`, reports: [{ message: `To load all the resources on a 3GFast network, it will take about 2.5s in optimal conditions (that is 1.5s more than the 1s target).`, severity: utils_types_1.Severity.error }], serverConfig: generateServerConfig(1) } ]; const connectionTypeTests = [ { name: 'Plain page loads fast enough on Dial', serverConfig: (0, utils_create_server_1.generateHTMLPage)() }, { name: `Page with 1 image doesn't load fast enough on Dial`, reports: [{ message: `To load all the resources on a Dial network, it will take about 50.7s in optimal conditions (that is 45.7s more than the 5s target).`, severity: utils_types_1.Severity.error }], serverConfig: generateServerConfig(1) } ]; (0, utils_tests_helpers_1.testHint)(hintPath, tests, { https: false }); (0, utils_tests_helpers_1.testHint)(hintPath, loadTimeTests, { hintOptions: { loadTime: 1 }, https: false }); (0, utils_tests_helpers_1.testHint)(hintPath, connectionTypeTests, { hintOptions: { connectionType: 'Dial' }, https: false });