@hint/hint-performance-budget
Version:
hint that that checks if a page passes a set performance budget
104 lines (103 loc) • 3.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
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;
}
}
serverConfig['/'] = (0, utils_create_server_1.generateHTMLPage)('', generateBody(imageCount));
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.7s in optimal conditions (that is -0.3s 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.2s in optimal conditions (that is 2.2s 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.6s in optimal conditions (that is 17.6s 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.7s in optimal conditions (that is 1.7s 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.8s in optimal conditions (that is 45.8s more than the 5s target).`,
severity: utils_types_1.Severity.error
}],
serverConfig: generateServerConfig(1)
}
];
(0, utils_tests_helpers_1.testHint)(hintPath, tests, { https: true });
(0, utils_tests_helpers_1.testHint)(hintPath, loadTimeTests, {
hintOptions: { loadTime: 1 },
https: true
});
(0, utils_tests_helpers_1.testHint)(hintPath, connectionTypeTests, {
hintOptions: { connectionType: 'Dial' },
https: true
});