Skip to content

Commit

Permalink
tests(smoke): test array _includes and lhr.timing (#13619)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed Feb 3, 2022
1 parent 19d95cc commit 83e2d3c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lighthouse-cli/test/smokehouse/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ Individual elements of an array can be asserted by using numeric properties in a

However, if an array literal is used as the expectation, an extra condition is enforced that the actual array _must_ have the same length as the provided expected array.

Arrays can be checked against a subset of elements using the special `_includes` property. The value of `_includes` _must_ be an array. Each assertion in `_includes` will remove the matching item from consideration for the rest.

**Examples**:
| Actual | Expected | Result |
| -- | -- | -- |
| `[{url: 'http://badssl.com'}, {url: 'http://example.com'}]` | `{1: {url: 'http://example.com'}}` | ✅ PASS |
| `[{timeInMs: 5}, {timeInMs: 15}]` | `{length: 2}` | ✅ PASS |
| `[{timeInMs: 5}, {timeInMs: 15}]` | `{_includes: [{timeInMs: 5}]}` | ✅ PASS |
| `[{timeInMs: 5}, {timeInMs: 15}]` | `{_includes: [{timeInMs: 5}, {timeInMs: 5}]}` | ❌ FAIL |
| `[{timeInMs: 5}, {timeInMs: 15}]` | `[{timeInMs: 5}]` | ❌ FAIL |

### Special environment checks
Expand Down
36 changes: 36 additions & 0 deletions lighthouse-cli/test/smokehouse/report-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,35 @@ function findDifference(path, actual, expected) {
const keyPath = path + keyAccessor;
const expectedValue = expected[key];

if (key === '_includes') {
if (!Array.isArray(expectedValue)) throw new Error('Array subset must be array');
if (!Array.isArray(actual)) {
return {
path,
actual: 'Actual value is not an array',
expected,
};
}

const actualCopy = [...actual];
for (const expectedEntry of expectedValue) {
const matchingIndex =
actualCopy.findIndex(actualEntry => !findDifference(keyPath, actualEntry, expectedEntry));
if (matchingIndex !== -1) {
actualCopy.splice(matchingIndex, 1);
continue;
}

return {
path,
actual: 'Item not found in array',
expected: expectedEntry,
};
}

continue;
}

const actualValue = actual[key];
const subDifference = findDifference(keyPath, actualValue, expectedValue);

Expand Down Expand Up @@ -305,6 +334,12 @@ function collateResults(localConsole, actual, expected) {
return makeComparison(auditName + ' audit', actualResult, expectedResult);
});

const timingAssertions = [];
if (expected.lhr.timing) {
const comparison = makeComparison('timing', actual.lhr.timing, expected.lhr.timing);
timingAssertions.push(comparison);
}

/** @type {Comparison[]} */
const requestCountAssertion = [];
if (expected.networkRequests) {
Expand All @@ -322,6 +357,7 @@ function collateResults(localConsole, actual, expected) {
...requestCountAssertion,
...artifactAssertions,
...auditAssertions,
...timingAssertions,
];
}

Expand Down
3 changes: 3 additions & 0 deletions types/smokehouse.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ declare global {
code?: any;
message?: any;
};
timing?: {
entries?: any
}
}

export type ExpectedRunnerResult = {
Expand Down

0 comments on commit 83e2d3c

Please sign in to comment.