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(gather-runner): type check #10215

Merged
merged 26 commits into from
Jan 14, 2020
Merged

tests(gather-runner): type check #10215

merged 26 commits into from
Jan 14, 2020

Conversation

connorjclark
Copy link
Collaborator

No description provided.

@@ -29,8 +53,16 @@ declare global {
/** Make optional all properties on T and any properties on object properties of T. */
type RecursivePartial<T> = {
[P in keyof T]+?:
// If type is a union, map each individual component and transform the resultant tuple back into a union.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Collaborator Author

@connorjclark connorjclark Jan 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was needed for types such as:

export interface Json {
    passes: PassJson[] | null;
}

It would otherwise not recuse into RecursivePartial<PassJson>, as PassJson[] | null fails all the conditional checks and just returns itself (T[P]).

Note, if the property is optional (and LH.Config.Json['passes'] actually is), SplitTypes<T> turns it into [undefined, PassJson[], null], so no special handling needed for that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment in the code with this concrete example? This helped a lot :)

@GoogleChrome GoogleChrome deleted a comment Jan 13, 2020
Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly lgtm though the meta question is a big one :)

@@ -787,20 +858,21 @@ describe('GatherRunner', function() {

return GatherRunner.run(config.passes, options)
.then(artifacts => {
// @ts-ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at this point this feels like an example of a test that is pointless with type checking :)

we could start testing all kinds of properties to make sure they're not set
assert.equal(artifacts.traceOfTab, undefined)
assert.equal(artifacts.BabyYoda, undefined)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had the same thought, just wanted someone else to notice too :)

lighthouse-core/test/gather/gather-runner-test.js Outdated Show resolved Hide resolved
types/externs.d.ts Show resolved Hide resolved
@@ -29,8 +53,16 @@ declare global {
/** Make optional all properties on T and any properties on object properties of T. */
type RecursivePartial<T> = {
[P in keyof T]+?:
// If type is a union, map each individual component and transform the resultant tuple back into a union.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment in the code with this concrete example? This helped a lot :)

// Guard against large string unions, which would be unreasonable to support (much more than 3 components is common).
SplitType<T[P]> extends string[] ? T[P] :
GetLength<SplitType<T[P]>> extends 2|3|4 ? RecursivePartialUnion<T[P]>[number] :
// Recurse into arrays.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these comments are great 👍

return /** @type {(...args: RecursivePartial<TParams>) => TReturn} */ (fn);
}

const GatherRunner = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meta question: do we still end up catching that many type bugs if we're mutating the type signature of the class under test so much?

should we be creating default configs/passes/etc to modify for each instead to maintain the integrity of the test?

Copy link
Collaborator Author

@connorjclark connorjclark Jan 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were no type bugs that were discovered from this particular file. Perhaps other files will have some (IIRC, driver-test had one).

The larger benefit IMO is in writing new tests, modifying existing ones, or just understanding current ones in the IDE (easier with types).

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@connorjclark connorjclark merged commit 6fba15d into master Jan 14, 2020
@connorjclark connorjclark deleted the ts-gather-runner-test branch January 14, 2020 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants