From 5c6632746070e682c4cdc23fb62385c1739f4a67 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Fri, 1 Mar 2019 11:36:57 -0800 Subject: [PATCH] test(smokehouse): +/- operator (#7343) --- .../byte-efficiency/expectations.js | 10 +++++++--- lighthouse-cli/test/smokehouse/smokehouse.js | 19 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js b/lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js index 2156f2e9f03e..2c4cc4bdb9b6 100644 --- a/lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js +++ b/lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js @@ -26,9 +26,13 @@ module.exports = [ details: { overallSavingsBytes: '>45000', overallSavingsMs: '>500', - items: { - length: 1, - }, + items: [ + { + url: 'http://localhost:10200/byte-efficiency/script.js', + wastedBytes: '46481 +/- 100', + wastedPercent: '87 +/- 5', + }, + ], }, }, 'unused-css-rules': { diff --git a/lighthouse-cli/test/smokehouse/smokehouse.js b/lighthouse-cli/test/smokehouse/smokehouse.js index d7f5ff316604..be6534d35bf8 100755 --- a/lighthouse-cli/test/smokehouse/smokehouse.js +++ b/lighthouse-cli/test/smokehouse/smokehouse.js @@ -34,8 +34,12 @@ const PROTOCOL_TIMEOUT_EXIT_CODE = 67; const PAGE_HUNG_EXIT_CODE = 68; const INSECURE_DOCUMENT_REQUEST_EXIT_CODE = 69; const RETRIES = 3; -const NUMERICAL_EXPECTATION_REGEXP = /^(<=?|>=?)((\d|\.)+)$/; const VERBOSE = Boolean(process.env.LH_SMOKE_VERBOSE); +const NUMBER_REGEXP = /(?:\d|\.)+/.source; +const OPS_REGEXP = /<=?|>=?|\+\/-/.source; +// An optional number, optional whitespace, an operator, optional whitespace, a number. +const NUMERICAL_EXPECTATION_REGEXP = + new RegExp(`^(${NUMBER_REGEXP})?\\s*(${OPS_REGEXP})\\s*(${NUMBER_REGEXP})$`); /** * Attempt to resolve a path locally. If this fails, attempts to locate the path @@ -149,17 +153,18 @@ function runLighthouse(url, configPath, isDebug) { function matchesExpectation(actual, expected) { if (typeof actual === 'number' && NUMERICAL_EXPECTATION_REGEXP.test(expected)) { const parts = expected.match(NUMERICAL_EXPECTATION_REGEXP); - const operator = parts[1]; - const number = parseFloat(parts[2]); + const [, prefixNumber, operator, postfixNumber] = parts; switch (operator) { case '>': - return actual > number; + return actual > postfixNumber; case '>=': - return actual >= number; + return actual >= postfixNumber; case '<': - return actual < number; + return actual < postfixNumber; case '<=': - return actual <= number; + return actual <= postfixNumber; + case '+/-': + return Math.abs(actual - prefixNumber) <= postfixNumber; default: throw new Error(`unexpected operator ${operator}`); }