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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate some files to typescript #848

Merged

Conversation

marcosvega91
Copy link
Member

@marcosvega91 marcosvega91 commented Dec 10, 2020

follow-up #614

I have started migrating @testing-library/dom to typescript. The idea is also to complete step by step PR #834

Types are not generated until everything is completed

Any suggestions and advice are always welcome 馃檹馃徑

I want to add more files to this PR.

What: File migrated

  • config.js
  • matches.js
  • label-helpers.js

Why: Is cool and fun

How:

Checklist:

  • Documentation added to the
    docs site
  • Tests
  • Typescript definitions updated
  • Ready to be merged

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 10, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit d15238b:

Sandbox Source
react-testing-library-examples Configuration

@codecov
Copy link

codecov bot commented Dec 10, 2020

Codecov Report

Merging #848 (d15238b) into master (f753c8a) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #848   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           26        26           
  Lines          941       945    +4     
  Branches       289       290    +1     
=========================================
+ Hits           941       945    +4     
Impacted Files Coverage 螖
src/events.js 100.00% <酶> (酶)
src/config.ts 100.00% <100.00%> (酶)
src/label-helpers.ts 100.00% <100.00%> (酶)
src/matches.ts 100.00% <100.00%> (酶)

Continue to review full report at Codecov.

Legend - Click here to learn more
螖 = absolute <relative> (impact), 酶 = not affected, ? = missing data
Powered by Codecov. Last update f753c8a...d15238b. Read the comment docs.

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

Very cool @marcosvega91 馃憦

@@ -1,4 +1,4 @@
import {configure, getConfig} from '../config'
import {configure, getConfig} from '../config.ts'
Copy link
Member

Choose a reason for hiding this comment

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

Interesting. Is the extension required? I didn't think it was and would prefer to not have to specify it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't like too, but I didn't find a way to not specify it. eslint gave me some trouble.

Copy link
Member

Choose a reason for hiding this comment

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

We can deal with that later. Not a ship stopper.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll leave the point open, maybe someone could help

src/config.ts Outdated Show resolved Hide resolved
kentcdodds
kentcdodds previously approved these changes Dec 10, 2020
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

We need to remember that the types directory should be completely removed when this migration is finished. There's no reason to keep it around once we can generate the types from the source.

But for now, this is perfect 馃憤

@kentcdodds
Copy link
Member

Personally, I think let's get this merged, then we can continue to iterate and avoid large branches.

@marcosvega91
Copy link
Member Author

I have only one more file

@marcosvega91
Copy link
Member Author

ready for now

kentcdodds
kentcdodds previously approved these changes Dec 10, 2020
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

Looking good to me. I don't plan to merge this myself, but I did leave a few thoughts and ideas that aren't a big deal either way.

src/label-helpers.ts Outdated Show resolved Hide resolved
Comment on lines 32 to 33
} else if ('value' in node) {
textContent = node.value
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} else if ('value' in node) {
textContent = node.value

} else {
textContent = node.value || node.textContent
textContent = node.textContent
Copy link
Member

Choose a reason for hiding this comment

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

Could this work?

Suggested change
textContent = node.textContent
textContent = node?.value ?? node.textContent

Or even

Suggested change
textContent = node.textContent
textContent = node.value ?? node.textContent

Copy link
Member Author

Choose a reason for hiding this comment

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

The problem here is that node can't be null but at the same time only input has value.
So doing node?.value is not correct because node is not nulla and node.value is not correct because input doesn't have a value

src/config.ts Outdated Show resolved Hide resolved
src/label-helpers.ts Outdated Show resolved Hide resolved
Comment on lines 36 to 45
function getRealLabels(element) {
if (element.labels !== undefined) return element.labels ?? []
function getRealLabels(element: Element | HTMLInputElement) {
// for old browsers
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if ('labels' in element && element.labels !== undefined)
return element.labels ?? []
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer we don't change any runtime behavior but rather override the typechecker with type-casting or @ts-expect-error. Right now I can't know from the review whether this changes runtime behavior, or was unsound, or just TS being overly strict.

Copy link
Member Author

Choose a reason for hiding this comment

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

The problem here is the labels can be null and not undefined. Btw I think that on old browser like ie labels is undefined and not null

Copy link
Member

Choose a reason for hiding this comment

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

Looks to me covered all these cases and TypeScript is not able to narrow. Let's keep the runtime and annotate the compile time properly.

Copy link
Member Author

Choose a reason for hiding this comment

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

The problem is that the linter is giving the problem, so @ts-expect-error will not work

Copy link
Member

Choose a reason for hiding this comment

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

Ignore the linter.

src/label-helpers.ts Outdated Show resolved Hide resolved
src/utils.ts Outdated Show resolved Hide resolved
tests/jest.config.dom.js Outdated Show resolved Hide resolved
types/matches.d.ts Outdated Show resolved Hide resolved
types/utils.d.ts Outdated Show resolved Hide resolved
eps1lon
eps1lon previously approved these changes Dec 11, 2020
Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

Proposed follow-up in marcosvega91#2

Should have the minimal set of runtime changes. Kept the switch from match to test which could be merged regardless of types.

commit 6563b96
Author: eps1lon <silbermann.sebastian@gmail.com>
Date:   Fri Dec 11 14:33:36 2020 +0100

    Remove .ts extensions

commit 6c08f92
Author: eps1lon <silbermann.sebastian@gmail.com>
Date:   Fri Dec 11 14:17:30 2020 +0100

    Polish

    - revert most runtime changes
    - test over exec because semantics
@eps1lon eps1lon changed the title chore: migrate some files to typescript feat migrate some files to typescript Dec 11, 2020
@eps1lon eps1lon changed the title feat migrate some files to typescript feat: migrate some files to typescript Dec 11, 2020
@eps1lon
Copy link
Member

eps1lon commented Dec 11, 2020

Forgot to update the PR title of the recently merged #821. Changing this PR to a feature (can always do that) so that we can kick of a release.

@marcosvega91
Copy link
Member Author

thanks for the help @eps1lon :)

Comment on lines -60 to +63
"extends": "./node_modules/kcd-scripts/eslint.js",
"extends": [
"./node_modules/kcd-scripts/eslint.js",
"plugin:import/typescript"
],
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I see, the strange thing is that is should already be in kcd

Copy link
Member

Choose a reason for hiding this comment

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

It should be. That's an oversight

@eps1lon eps1lon merged commit accb6cc into testing-library:master Dec 11, 2020
@github-actions
Copy link

馃帀 This PR is included in version 7.29.0 馃帀

The release is available on:

Your semantic-release bot 馃摝馃殌

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.

None yet

3 participants