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

breaking: Migrate codebase to typescript #101

Merged
merged 28 commits into from
Sep 30, 2022

Conversation

Ne3l
Copy link
Contributor

@Ne3l Ne3l commented Aug 15, 2022

What:

closes #81.

Why:

Typescript provide type safety and ease of use for developers.

How:

As the repository is quite small, migrated all in once.

Use typescript compiler instead of babel to create the dist build. (Babel with react-native-metro preset is optimised to run JS on hermes/jsc not node but it can be configured for it)

Still some parts of the code are not 100% solved but i believe this is a good base to iterate on.

Checklist:

This is a BREAKING CHANGE

@codecov
Copy link

codecov bot commented Aug 15, 2022

Codecov Report

Merging #101 (bb67f0f) into main (d38fc80) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main      #101   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            8         8           
  Lines          149       151    +2     
  Branches        45        47    +2     
=========================================
+ Hits           149       151    +2     
Flag Coverage Δ
node-14 100.00% <100.00%> (ø)
node-16 100.00% <100.00%> (ø)
node-18 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/extend-expect.ts 100.00% <100.00%> (ø)
src/to-be-disabled.ts 100.00% <100.00%> (ø)
src/to-be-empty-element.ts 100.00% <100.00%> (ø)
src/to-contain-element.ts 100.00% <100.00%> (ø)
src/to-have-prop.ts 100.00% <100.00%> (ø)
src/to-have-style.ts 100.00% <100.00%> (ø)
src/to-have-text-content.ts 100.00% <100.00%> (ø)
src/utils.ts 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Collaborator

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

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

@Ne3l looks very promising 🚀

I've done first pass of the review and added some TS suggestions for improving typing.

Additionally you probably should use // @ts-expect-error instead of // @ts-ignore when we know that we are hacking TS.

extend-expect.d.ts Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
src/__tests__/to-contain-element.tsx Outdated Show resolved Hide resolved
src/__tests__/to-have-prop.tsx Show resolved Hide resolved
src/__tests__/to-have-style.tsx Show resolved Hide resolved
src/utils.ts Outdated Show resolved Hide resolved
src/utils.ts Outdated Show resolved Hide resolved
src/to-have-prop.ts Outdated Show resolved Hide resolved
src/to-have-prop.ts Outdated Show resolved Hide resolved
src/to-be-disabled.ts Show resolved Hide resolved
@Ne3l
Copy link
Contributor Author

Ne3l commented Aug 19, 2022

@mdjastrzebski Thanks for reviewing, i'm on holidays now but will take a look at the MR at the beginning of september

@Ne3l Ne3l force-pushed the typescriptMigration branch from 7551917 to ffa6b59 Compare September 11, 2022 18:57
@Ne3l
Copy link
Contributor Author

Ne3l commented Sep 11, 2022

@mdjastrzebski I think i answer/fix all the open threads, please take a look.

.husky/pre-commit Outdated Show resolved Hide resolved
extend-expect.d.ts Outdated Show resolved Hide resolved
src/utils.ts Outdated Show resolved Hide resolved
src/to-have-style.ts Outdated Show resolved Hide resolved
@Ne3l Ne3l force-pushed the typescriptMigration branch from e8e6c9b to 1c20f5c Compare September 18, 2022 19:37
@Ne3l
Copy link
Contributor Author

Ne3l commented Sep 18, 2022

@mdjastrzebski Not sure what's the etiquette on this repo, but can you resolve the comments that are fixed and take a look at the rest? i've rebased the changes of #110.

src/to-have-style.ts Outdated Show resolved Hide resolved
@mdjastrzebski
Copy link
Collaborator

@Ne3l I've closed comments that I think are resolved. Sometimes it's hard to easily figure out if the given comment is solved or not, as they disappear from "Files changed" view.

@Ne3l Ne3l force-pushed the typescriptMigration branch from 9c6ff77 to a105a87 Compare September 26, 2022 18:05
@Ne3l
Copy link
Contributor Author

Ne3l commented Sep 26, 2022

Hi @mdjastrzebski it's me again 😅 i have fixed most of your comments except the narrow one as i'm not sure i understand what do you mean.

Lets hope this is the good and we can merge this typescript setup as rebasing each new commit is a bit of a pain in the ass.

@mdjastrzebski
Copy link
Collaborator

@Ne3l I've reviewed the changes and made some minor tweaks to adjust typing or restore original logic where appropriate. Pls take a look at the changes if everything looks good, we can plan merging it tomorrow.

I am wondering whether to release this as a new major version/breaking change. The reasoning behind it is that we tweaked some external types and this might cause user tests to fail.

On the other hand the logic of all of the matchers should stay the same, so minor version bump could also do.

@mdjastrzebski mdjastrzebski changed the title refactor: Migrate codebase to typescript breaking: Migrate codebase to typescript Sep 28, 2022
result = result.concat(collectChildrenText(child));
}
const result: string[] = [];
element.children.forEach((child) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

out of curiosity, why the forEach instead of the for of?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No particular reason beside subjectively slightly easier to read.


type Style = TextStyle | ViewStyle | ImageStyle;
type StyleLike = Record<string, unknown>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

why the styleLike? passing an undefined as a styleProp is valid but the flatten won't return a Record

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for pointing out potential undefined return from StyleSheet.flatten() I've added check for that.

Regarding why Record and not concrete Style, it has to do with Object.keys() not handling well Style, so we either need to we sprinkle Object.keys or StyleSheet.flatten with as typecasts. Since flattened style is essential Record<string, any> or undefined, then it makes sense to use Record as it does not quire additional typecasts in the checking functions downstream.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for heads up, I think we good for now and we'll have to adjust JS implementation when something new comes to replace flatten().

@@ -6,15 +6,26 @@ exports[`.toHaveStyle handles negative test cases 1`] = `
- Expected

backgroundColor: blue;
- transform: [{"scale": 1}];
+ transform: [{"scale": 2}];"
transform: [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i had the doubt when doing the change, do we want to output everything or just the result which is different than the expectation?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The idea behind this is that particular style attributes are not divisible, they need to match as units.

Normally this is not an issue as most style attributes are string, number, etc. The only two Array attributes are transform and fontVariant and we want to output the whole array in this case.

@Ne3l
Copy link
Contributor Author

Ne3l commented Sep 28, 2022

LGTM

regarding the imports i saw you done some ordering. There is an eslint plugin for it that you may want to use https://eslint.org/docs/latest/rules/sort-imports

@mdjastrzebski mdjastrzebski merged commit 0896cd2 into testing-library:main Sep 30, 2022
@github-actions
Copy link

🎉 This PR is included in version 5.0.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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor: migrate code to TypeScript
3 participants