Skip to content

Commit

Permalink
feat(utils): add support for default aggregationMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Oct 9, 2019
1 parent c0329f1 commit 6c098cd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/utils/src/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ function resolveAssertionOptionsAndLhrs(baseOptions, unfilteredLhrs) {
optionsToUse = _.merge(_.cloneDeep(presetData), optionsToUse);
}

const {assertions = {}, matchingUrlPattern: urlPattern} = optionsToUse;
const {assertions = {}, matchingUrlPattern: urlPattern, aggregationMethod} = optionsToUse;
const lhrs = urlPattern
? unfilteredLhrs.filter(lhr => doesLHRMatchPattern(urlPattern, lhr))
: unfilteredLhrs;
Expand All @@ -312,6 +312,7 @@ function resolveAssertionOptionsAndLhrs(baseOptions, unfilteredLhrs) {
assertions,
auditsToAssert,
medianLhrs,
aggregationMethod,
lhrs: lhrs,
url: (lhrs[0] && lhrs[0].finalUrl) || '',
};
Expand All @@ -323,10 +324,14 @@ function resolveAssertionOptionsAndLhrs(baseOptions, unfilteredLhrs) {
* @return {AssertionResult[]}
*/
function getAllFilteredAssertionResults(baseOptions, unfilteredLhrs) {
const {assertions, auditsToAssert, medianLhrs, lhrs, url} = resolveAssertionOptionsAndLhrs(
baseOptions,
unfilteredLhrs
);
const {
assertions,
auditsToAssert,
medianLhrs,
lhrs,
url,
aggregationMethod,
} = resolveAssertionOptionsAndLhrs(baseOptions, unfilteredLhrs);

// If we don't have any data, just return early.
if (!lhrs.length) return [];
Expand All @@ -338,14 +343,14 @@ function getAllFilteredAssertionResults(baseOptions, unfilteredLhrs) {
const [level, assertionOptions] = normalizeAssertion(assertions[assertionKey]);
if (level === 'off') continue;

const lhrsToUseForAudit =
assertionOptions.aggregationMethod === 'median-run' ? medianLhrs : lhrs;
const options = {aggregationMethod, ...assertionOptions};
const lhrsToUseForAudit = options.aggregationMethod === 'median-run' ? medianLhrs : lhrs;
const auditResults = lhrsToUseForAudit.map(lhr => lhr.audits[auditId]);
const assertionResults = getAssertionResultsForAudit(
auditId,
auditProperty,
auditResults,
assertionOptions
options
);

for (const result of assertionResults) {
Expand Down
30 changes: 30 additions & 0 deletions packages/utils/test/assertions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ describe('getAllAssertionResults', () => {
});

describe('aggregationMethod', () => {
it('should default to aggregationMethod optimistic', () => {
const assertions = {
'first-contentful-paint': ['warn', {minScore: 1}],
'network-requests': ['warn', {maxLength: 1}],
};

const results = getAllAssertionResults({assertions}, lhrs);
expect(results).toMatchObject([{actual: 0.8}, {actual: 2}]);
});

it('should use aggregationMethod optimistic', () => {
const assertions = {
'first-contentful-paint': ['warn', {aggregationMethod: 'optimistic', minScore: 1}],
Expand Down Expand Up @@ -243,6 +253,26 @@ describe('getAllAssertionResults', () => {
]);
});

it('should use file-wide default when set', () => {
const assertions = {
'first-contentful-paint': ['warn', {minScore: 1}],
'network-requests': ['warn', {maxLength: 1}],
};

const results = getAllAssertionResults({assertions, aggregationMethod: 'pessimistic'}, lhrs);
expect(results).toMatchObject([{actual: 0.6}, {actual: 4}]);
});

it('should override file-wide default when set', () => {
const assertions = {
'first-contentful-paint': ['warn', {minScore: 1, aggregationMethod: 'pessimistic'}],
'network-requests': ['warn', {maxLength: 1}],
};

const results = getAllAssertionResults({assertions, aggregationMethod: 'median'}, lhrs);
expect(results).toMatchObject([{actual: 0.6}, {actual: 3}]);
});

it('should handle partial failure with mode optimistic', () => {
const assertions = {
'first-contentful-paint': ['warn', {aggregationMethod: 'optimistic'}],
Expand Down
1 change: 1 addition & 0 deletions types/assert.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ declare global {

export interface BaseOptions {
matchingUrlPattern?: string;
aggregationMethod?: AssertionAggregationMethod;
preset?: 'lighthouse:all' | 'lighthouse:recommended';
assertions?: Assertions;
}
Expand Down

0 comments on commit 6c098cd

Please sign in to comment.