Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests(smoke): test array _includes and lhr.timing #13619

Merged
merged 3 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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