Skip to content

Commit

Permalink
tests(smokehouse): assert on expected array length (#9292)
Browse files Browse the repository at this point in the history
Co-Authored-By: Brendan Kenny <[email protected]>
  • Loading branch information
connorjclark and brendankenny authored Jul 9, 2019
1 parent 5e52dcc commit 4c658d2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ module.exports = [
transferSize: 53181,
resourceSize: 52997,
},
{
url: 'http://localhost:10200/favicon.ico',
},
],
},
},
Expand Down
14 changes: 10 additions & 4 deletions lighthouse-cli/test/smokehouse/error-expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
*/
'use strict';

// Just using `[]` actually asserts for an empty array.
// Use this expectation object to assert an array with at least one element.
const NONEMPTY_ARRAY = {
length: '>0',
};

/**
* Expected Lighthouse audit values for sites with various errors.
*/
Expand All @@ -24,10 +30,10 @@ module.exports = [
artifacts: {
PageLoadError: {code: 'PAGE_HUNG'},
devtoolsLogs: {
'pageLoadError-defaultPass': [/* ... */],
'pageLoadError-defaultPass': NONEMPTY_ARRAY,
},
traces: {
'pageLoadError-defaultPass': {traceEvents: [/* ... */]},
'pageLoadError-defaultPass': {traceEvents: NONEMPTY_ARRAY},
},
},
},
Expand All @@ -46,10 +52,10 @@ module.exports = [
artifacts: {
PageLoadError: {code: 'INSECURE_DOCUMENT_REQUEST'},
devtoolsLogs: {
'pageLoadError-defaultPass': [/* ... */],
'pageLoadError-defaultPass': NONEMPTY_ARRAY,
},
traces: {
'pageLoadError-defaultPass': {traceEvents: [/* ... */]},
'pageLoadError-defaultPass': {traceEvents: NONEMPTY_ARRAY},
},
},
},
Expand Down
12 changes: 12 additions & 0 deletions lighthouse-cli/test/smokehouse/smokehouse-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ function findDifference(path, actual, expected) {
}
}

// If the expected value is an array, assert the length as well.
// This still allows for asserting that the first n elements of an array are specified elements,
// but requires using an object literal (ex: {0: x, 1: y, 2: z} matches [x, y, z, q, w, e] and
// {0: x, 1: y, 2: z, length: 5} does not match [x, y, z].
if (Array.isArray(expected) && actual.length !== expected.length) {
return {
path: `${path}.length`,
actual,
expected,
};
}

return null;
}

Expand Down

0 comments on commit 4c658d2

Please sign in to comment.